存储过程中的SQL Server 2005外部联接问题

时间:2009-10-30 19:40:01

标签: sql sql-server sql-server-2005 tsql stored-procedures

Good Afternoon All,

我的SQL Server 2005 DB中有两个表,Main和MSDRGWEIGHTS。我想创建一个存储过程,使用MSDRGWEIGHTS中的适当值更新Main.RelWeight。我已将以下代码编写为存储过程的一部分:

UPDATE MAIN 
left outer join MSDRGWEIGHTS AS W ON MAIN.MSDRG=W.MSDRG
SET M.RELWEIGHT =
CASE
WHEN M.DISCHARGEDATE BETWEEN 20071001 AND 20080930 THEN W.WEIGHTSOCT07
WHEN M.DISCHARGEDATE BETWEEN 20081001 AND 20090930 THEN W.WEIGHTSOCT08
END
END
GO

当我执行此代码时,收到错误消息,指出关键字左侧的语法不正确。 任何人都可以建议使用存储过程更新RELWEIGHT的正确方法吗?或者,这是否超出了sproc的范围?

谢谢, SID

1 个答案:

答案 0 :(得分:4)

    UPDATE 
          m 
     SET m.RELWEIGHT= CASE
                        WHEN M.DISCHARGEDATE BETWEEN 20071001 AND 20080930 THEN W.WEIGHTSOCT07WHEN   
                       WHEN M.DISCHARGEDATE BETWEEN 20081001 AND 20090930 THEN W.WEIGHTSOCT08
    END 
    FROM 
          MAIN m 
   INNER JOIN MSDRGWEIGHTS mw 
   ON 
   mw.MSDRG = m.MSDRG