开发者

SQL Update a table based on join with anther table

开发者 https://www.devze.com 2023-03-24 08:17 出处:网络
I am trying to update a tab开发者_如何学Pythonle by joining the values with another table. Here\'s my query so far.

I am trying to update a tab开发者_如何学Pythonle by joining the values with another table. Here's my query so far.

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

When i run this, i get this

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

Thanks


If you expect to update a large fraction of the rows in LOGIN, it will likely be more efficient to use an 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' )

If you are updating a relatively small fraction of the rows in LOGIN, Yahia's approach of using an IN would likely be more efficient

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


try

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

Another more complicated option see http://geekswithblogs.net/WillSmith/archive/2008/06/18/oracle-update-with-join-again.aspx


Your syntax is not quite correct. This Wikipedia article has number of examples that you can choose from.

For example

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

精彩评论

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

关注公众号