1

我正在尝试在 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 提出建议吗?

4

2 回答 2

0

单程

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)
于 2011-08-01T14:44:12.603 回答
0

它通常有助于定义“不起作用”的含义(例如,您遇到了什么错误)以及指定您正在使用的 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
  );
于 2011-08-01T14:47:14.030 回答