开发者

View from DB2 to SQL Server 2005

开发者 https://www.devze.com 2023-03-24 02:45 出处:网络
I\'m attempting to move a view between DB2 and SQL Server. CREATE VIEW msu.bad_bus_cnty_st_mstr AS SELECT id,

I'm attempting to move a view between DB2 and 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%'); 

The vie开发者_如何学Gow works in DB2, but doesn't work with SQL Server because of the WHERE clause. Can I have a recommendation on how to rewrite this view to work with SQL Server?


It usually helps to define what "doesn't work" means (e.g. what error did you get) and also to specify the version of SQL Server you are using.

Unfortunately SQL Server doesn't support IN() with more than one clause. However you can re-write your view this way:

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
  );


one way

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号