Db Unix

-- Insert new records into master table

INSERT INTO $master_table (

    parent,

    parent_sys_class_name,

    child_sys_class_name,

    child_u_cmdb_ci_used_for,

    parent_correlation_id,

    type,

    child_correlation_id,

    child,

    modified_date

)

SELECT

    c.parent,

    c.parent_sys_class_name,

    c.child_sys_class_name,

    c.child_u_cmdb_ci_used_for,

    c.parent_correlation_id,

    c.type,

    c.child_correlation_id,

    c.child,

    c.modified_date

FROM $child_table c

LEFT JOIN $master_table m ON c.child_correlation_id = m.child_correlation_id

WHERE m.child_correlation_id IS NULL;


-- Log inserted rows

SPOOL $inserted_rows_file

SELECT

    c.parent,

    c.parent_sys_class_name,

    c.child_sys_class_name,

    c.child_u_cmdb_ci_used_for,

    c.parent_correlation_id,

    c.type,

    c.child_correlation_id,

    c.child,

    c.modified_date

FROM $child_table c

LEFT JOIN $master_table m ON c.child_correlation_id = m.child_correlation_id

WHERE m.child_correlation_id IS NULL;

SPOOL OFF;


-- Update modified records in master table

MERGE INTO $master_table m

USING $child_table c

ON (m.child_correlation_id = c.child_correlation_id)

WHEN MATCHED THEN

UPDATE SET

    m.parent = c.parent,

    m.parent_sys_class_name = c.parent_sys_class_name,

    m.child_sys_class_name = c.child_sys_class_name,

    m.child_u_cmdb_ci_used_for = c.child_u_cmdb_ci_used_for,

    m.parent_correlation_id = c.parent_correlation_id,

    m.type = c.type,

    m.child_correlation_id = c.child_correlation_id,

    m.child = c.child,

    m.modified_date = SYSDATE

WHERE

    m.parent != c.parent OR

    m.parent_sys_class_name != c.parent_sys_class_name OR

    m.child_sys_class_name != c.child_sys_class_name OR

    m.child_u_cmdb_ci_used_for != c.child_u_cmdb_ci_used_for OR

    m.parent_correlation_id != c.parent_correlation_id OR

    m.type != c.type OR

    m.child != c.child;


-- Log updated rows

SPOOL $updated_rows_file

SELECT

    c.parent,

    c.parent_sys_class_name,

    c.child_sys_class_name,

    c.child_u_cmdb_ci_used_for,

    c.parent_correlation_id,

    c.type,

    c.child_correlation_id,

    c.child,

    c.modified_date

FROM $child_table c

JOIN $master_table m ON c.child_correlation_id = m.child_correlation_id

WHERE

    m.parent != c.parent OR

    m.parent_sys_class_name != c.parent_sys_class_name OR

    m.child_sys_class_name != c.child_sys_class_name OR

    m.child_u_cmdb_ci_used_for != c.child_u_cmdb_ci_used_for OR

    m.parent_correlation_id != c.parent_correlation_id OR

    m.type != c.type OR

    m.child != c.child;

SPOOL OFF;




**"""



-- Update modified records in master table

MERGE INTO $master_table m

USING $child_table c

ON (m.child_correlation_id = c.child_correlation_id)

WHEN MATCHED THEN

UPDATE SET 

    m.parent = c.parent, 

    m.parent_sys_class_name = c.parent_sys_class_name,

    m.child_sys_class_name = c.child_sys_class_name,

    m.child_u_cmdb_ci_used_for = c.child_u_cmdb_ci_used_for, 

    m.parent_correlation_id = c.parent_correlation_id, 

    m.type = c.type, 

    m.child = c.child, 

    m.modified_date = SYSDATE

WHERE 

    m.parent != c.parent OR 

    m.parent_sys_class_name != c.parent_sys_class_name OR

    m.child_sys_class_name != c.child_sys_class_name OR 

    m.child_u_cmdb_ci_used_for != c.child_u_cmdb_ci_used_for OR 

    m.parent_correlation_id != c.parent_correlation_id OR 

    m.type != c.type OR 

    m.child != c.child;

Comments

Popular posts from this blog

SQL Loader V3

Standby redo log add