Skip to Main Content
  • Questions
  • Data copy from Staging to Application table

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: February 16, 2017 - 5:54 am UTC

Last updated: February 17, 2017 - 12:31 am UTC

Version: 11.2

Viewed 1000+ times

You Asked

Hi Team,

Can you pleas eheve alook ate below scenario and suggest any better alternative to achive it

I have Staging table - Tb_Staging_0001
Columns - Card_No, Address_1, .. Address_5, Credit_amount, Debit_amount, .... so on..

And Application table (tb_card_details) having exactly same columns as that of Staging table.

Daily job which move data from Staging to Application table.

Current Logic -
For every row in Tb_Staging_0001,
for (select * from Tb_Staging_0001)
Loop
it compare all columns with Application table.
like
IF Tb_Staging_0001.Address_1 != tb_card_details.Address_1
THEN
mark it as modified
END IF;
IF Tb_Staging_0001.Address_2 != tb_card_details.Address_2
THEN
mark it as modified
END IF;
..
so on

and mark those columns as Modified for audit purpose.
end loop;

Problem here is staging tables contains around millions of records, and in loop it becomes time comsuming (IF THEN for each loop)

Can you please suggest any better way, so that I can try it out.

and Connor said...

A MERGE should do the trick, using DECODE as a simple means to compare values

  merge into STAGING s
  using ( select * from APP_TABLE ) a
   on (  s.PK_COL1  = a.PK_COL1
     and s.PK_COL2  = a.PK_COL2  -- ie, primary key columns
     and ...
     )
  when matched then
  update set
    s.MODIFIED = 'YES'
  where   decode(s.COL1,a.COL1,1,0) = 0
     or   decode(s.COL2,a.COL2,1,0) = 0
     or   decode(s.COL3,a.COL3,1,0) = 0
     or   decode(s.COL4,a.COL4,1,0) = 0
     or   decode(s.COL5,a.COL5,1,0) = 0
     ...
     



Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Audit of Columns which are Modified

A reader, February 16, 2017 - 6:36 am UTC

Hi Connor,

Thanks for you reply.
I need a list of columns which are modified for Audit purpose.

E.g. If staging table is having 100 columns and only 20 columns are modified, I need to log entry of those 20 columns for Audit purpose.

It cannot be done in MERGE right?

Can you please help on this.
Connor McDonald
February 17, 2017 - 12:31 am UTC

Well...I dont know what structure you have for recording that, seeing as we have a complete lack of a test case :-)

But I'll assume that you have an additional set of columns "COL1_MODIFIED", "COL2_MODIFIED" etc...then you could extend the merge

merge into STAGING s
  using ( select * from APP_TABLE ) a
   on (  s.PK_COL1  = a.PK_COL1
     and s.PK_COL2  = a.PK_COL2  -- ie, primary key columns
     and ...
     )
  when matched then
  update set
    s.MODIFIED = 'YES',
    s.COL1_MODIFIED = decode(s.COL1,a.COL1,1,0),
    s.COL2_MODIFIED = decode(s.COL1,a.COL2,1,0),
    s.COL3_MODIFIED = decode(s.COL1,a.COL3,1,0),
    s.COL4_MODIFIED = decode(s.COL1,a.COL4,1,0)
    ...
  where   decode(s.COL1,a.COL1,1,0) = 0
     or   decode(s.COL2,a.COL2,1,0) = 0
     or   decode(s.COL3,a.COL3,1,0) = 0
     or   decode(s.COL4,a.COL4,1,0) = 0
     or   decode(s.COL5,a.COL5,1,0) = 0