简体   繁体   中英

Update one table from another table in pl\sql or oracle (only the rows changed from CDC table)

I wanted to write an update in Oracle 11g, where I want to update tableA with only the changes in TableCDC. I don't want to update all the rows in table A, I want to update only the rows which changed between TableA and TableCDC.

Update tableA
set (tableA.col1,tableA.col2,tableA.col3)
 = (select col1,Col2,Col3 from tableCDC
    where tableA.ID = tableCDC.ID
    and tableA.Year = tablecdc.Year)
where (tableA.col1 <> tablecdc.col1
or tableA.col2 <> tablecdc.col2
or tableA.col3 <> tablecdc.col3)

This is not working...how to write in the correct format?

Update tableA A
set (col1,col2,col3)
 = ( select CDC.col1,CDC.col2, CDC.Col3
     FROM TableCDC CDC
     WHERE A.ID = CDC.ID
     AND A.YEAR = CDC.YEAR
     AND 
     (A.col1 <> CDC.col1
      OR
      A.COL2 <> CDC.COL2
      OR
      A.COL3 <> CDC.COL3
     )
)
WHERE EXISTS 
(
SELECT 1
FROM TableCDC CDC
WHERE A.ID = CDC.ID
     AND A.YEAR = CDC.YEAR
     AND 
     (A.col1 <> CDC.col1
      OR
      A.COL2 <> CDC.COL2
      OR
      A.COL3 <> CDC.COL3
     )
);

you have syntax problem.try this.

 update tableA
    set (tableA.col1,tableA.col2,tableA.col3)=
    (
    select col1,col2,col3 from tableCDC 
    where (tableA.id = tableCDC.id and tableA.year=tableCDC.year 
    and (
    (tableA.col1<>tableCDC.col1) 
    or 
    (tableA.col2<>tableCDC.col2)
    or 
    (tableA.col3<>tableCDC.col3))
    ))
    where exists 
    (select 1 from tableCDC 
    where (tableA.id = tableCDC.id and tableA.year=tableCDC.year 
    and (
    (tableA.col1<>tableCDC.col1) 
    or 
    (tableA.col2<>tableCDC.col2)
    or 
    (tableA.col3<>tableCDC.col3))
    ));

for more details about update syntax in oracle just click here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM