【问题标题】:View from DB2 to SQL Server 2005从 DB2 查看到 SQL Server 2005
【发布时间】:2011-10-17 12:31:44
【问题描述】:

我正在尝试在 DB2 和 SQL Server 之间移动视图。

CREATE VIEW msu.bad_bus_cnty_st_mstr 
AS 
  SELECT id, 
         bus_cnty_cntry_cd, 
         bus_st, 
         bus_zip 
  FROM   summit.mstr 
  WHERE  ( bus_cnty_cntry_cd, bus_st ) IN (SELECT cnty_cntry_cd, 
                                                  st 
                                           FROM   uhelp.cnty_cntry_cd 
                                           WHERE 
         cnty_cntry_descr LIKE '%invalid%'); 

视图可以在 DB2 中使用,但由于 WHERE 子句,它不能在 SQL Server 中使用。我可以就如何重写此视图以使用 SQL Server 提出建议吗?

【问题讨论】:

    标签: sql-server db2 database-migration sql-view


    【解决方案1】:

    通常有助于定义“不起作用”的含义(例如,您遇到了什么错误)并指定您使用的 SQL Server 版本。

    不幸的是,SQL Server 不支持带有多个子句的 IN()。但是你可以用这种方式重写你的视图:

    ALTER VIEW msu.bad_bus_cnty_st_mstr 
    AS 
      SELECT id, 
             bus_cnty_cntry_cd, 
             bus_st, 
             bus_zip 
      FROM   summit.mstr AS mstr 
      WHERE EXISTS 
      (
         SELECT 1
            FROM uhelp.cnty_cntry_cd 
            WHERE cnty_cntry_descr LIKE '%invalid%'
             AND cnty_cntry_cd = mstr.bus_cnty_cntry_cd
             AND st = mstr.bus_st
      );
    

    【讨论】:

    • Msg 102,级别 15,状态 1,过程 bad_bus_cnty_st_mstr,第 8 行 ',' 附近的语法不正确。是我在 SQL2005 中收到的错误消息。
    • 您是否完全复制了我的代码?第 8 行没有逗号。我的代码中唯一的逗号是 SELECT 列表中的列之间的逗号。您是否在此处添加了额外的尾随逗号?
    • 抱歉,这是我收到的原始错误消息(例如,“不起作用”是什么意思)
    【解决方案2】:

    一种方式

    CREATE VIEW msu.bad_bus_cnty_st_mstr 
    AS 
      SELECT id, 
             bus_cnty_cntry_cd, 
             bus_st, 
             bus_zip 
      FROM   summit.mstr m
      WHERE  EXISTS( SELECT 1 FROM   uhelp.cnty_cntry_cd  c
      WHERE c.cnty_cntry_descr LIKE '%invalid%'
      AND c.bus_cnty_cntry_cd = m.bus_cnty_cntry_cd
      AND c.st = m.bus_st)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多