SQL更新基于与另一个表连接的表

时间:2011-07-31 20:48:10

标签: sql oracle oracle10g ora-00933

我正在尝试通过将值与另一个表连接来更新表。 到目前为止,这是我的查询。

    UPDATE LOGIN  SET LOGIN.DISABLED_IND = 'N', LOGIN.DREASON = 'Test'
        FROM  CONTACT
        WHERE CONTACT.CONTACT_ID = LOGIN.CONTACT_ID 
        AND CONTACT.RID ='abc'

当我运行时,我得到了这个

[Error Code: 933, SQL State: 42000] ORA-00933: SQL command not properly ended

由于

3 个答案:

答案 0 :(得分:2)

 UPDATE LOGIN L SET L.DISABLED_IND = 'N', L.DREASON = 'Test'
 WHERE L.CONTACT_ID 
 IN ( SELECT C.CONTACT_ID FROM CONTACT C WHERE C.CONTACT_ID = L.CONTACT_ID AND 
 C.RID='abc');

另一个更复杂的选项见http://geekswithblogs.net/WillSmith/archive/2008/06/18/oracle-update-with-join-again.aspx

答案 1 :(得分:2)

如果您希望更新LOGIN中的大部分行,则使用EXISTS可能会更有效

UPDATE LOGIN  l
   SET l.DISABLED_IND = 'N', 
       l.DREASON = 'Test'
 WHERE EXISTS (
    SELECT 1
      FROM CONTACT c
     WHERE c.CONTACT_ID = l.CONTACT_ID 
       AND c.RID ='abc' )

如果要更新LOGIN中相对较小部分的行,Yahia使用IN的方法可能会更有效

UPDATE LOGIN  l
   SET l.DISABLED_IND = 'N', 
       l.DREASON = 'Test'
 WHERE l.contact_id IN (
    SELECT c.contact_id
      FROM CONTACT c
     WHERE c.RID ='abc' )

答案 2 :(得分:0)

您的语法不太正确。此Wikipedia article有许多示例供您选择。

例如

UPDATE l
 SET l.DISABLED_IND = 'N', l.DREASON = 'Test'
 FROM LOGIN l
  JOIN CONTACT c
   ON c.CONTACT_ID = l.CONTACT_ID AND c.RID ='abc'