Oracle SQL update based on subquery between two tables

后端 未结 4 675
無奈伤痛
無奈伤痛 2020-12-31 11:51

I am currently writing update statements to keep a query-able table constantly up to date. The schema is identical between both tables and the contents are not important:

4条回答
  •  悲&欢浪女
    2020-12-31 12:05

    There are two ways to do what you are trying

    One is a Multi-column Correlated Update

    UPDATE PRODUCTION a
    SET (name, count) = (
      SELECT name, count
      FROM STAGING b
      WHERE a.ID = b.ID);
    

    DEMO

    You can use merge

    MERGE INTO PRODUCTION a
    USING ( select id, name, count 
              from STAGING ) b
    ON ( a.id = b.id )
    WHEN MATCHED THEN 
    UPDATE SET  a.name = b.name,
                a.count = b.count
    

    DEMO

提交回复
热议问题