Compare update sql

 -- Insert new records into master table

INSERT INTO &master_table (support_group, install_status, oracle_home, u_cmdb_ci_used_for, u_cmdb_owner_group, name, correlation_id, version, modified_date)

SELECT c.support_group, c.install_status, c.oracle_home, c.u_cmdb_ci_used_for, c.u_cmdb_owner_group, c.name, c.correlation_id, c.version, c.modified_date

FROM &child_table c

LEFT JOIN &master_table m ON c.correlation_id = m.correlation_id

WHERE m.correlation_id IS NULL;


-- Log inserted rows

SPOOL &inserted_rows_file

SELECT c.support_group, c.install_status, c.oracle_home, c.u_cmdb_ci_used_for, c.u_cmdb_owner_group, c.name, c.correlation_id, c.version, c.modified_date

FROM &child_table c

LEFT JOIN &master_table m ON c.correlation_id = m.correlation_id

WHERE m.correlation_id IS NULL;

SPOOL OFF;


-- Update modified records in master table

MERGE INTO &master_table m

USING &child_table c

ON (m.correlation_id = c.correlation_id)

WHEN MATCHED THEN

  UPDATE SET 

    m.support_group = c.support_group, 

    m.install_status = c.install_status, 

    m.oracle_home = c.oracle_home,

    m.u_cmdb_ci_used_for = c.u_cmdb_ci_used_for, 

    m.u_cmdb_owner_group = c.u_cmdb_owner_group, 

    m.name = c.name, 

    m.version = c.version, 

    m.modified_date = SYSDATE

  WHERE 

    m.support_group != c.support_group OR 

    m.install_status != c.install_status OR 

    m.oracle_home != c.oracle_home OR 

    m.u_cmdb_ci_used_for != c.u_cmdb_ci_used_for OR 

    m.u_cmdb_owner_group != c.u_cmdb_owner_group OR 

    m.name != c.name OR 

    m.version != c.version;


-- Log updated rows

SPOOL &updated_rows_file

SELECT c.support_group, c.install_status, c.oracle_home, c.u_cmdb_ci_used_for, c.u_cmdb_owner_group, c.name, c.correlation_id, c.version, c.modified_date

FROM &child_table c

JOIN &master_table m ON c.correlation_id = m.correlation_id

WHERE 

    m.support_group != c.support_group OR 

    m.install_status != c.install_status OR 

    m.oracle_home != c.oracle_home OR 

    m.u_cmdb_ci_used_for != c.u_cmdb_ci_used_for OR 

    m.u_cmdb_owner_group != c.u_cmdb_owner_group OR 

    m.name != c.name OR 

    m.version != c.version;

SPOOL OFF;

Comments

Popular posts from this blog

SQL Loader V3

Db Unix

Standby redo log add