Compare Pre-Post tables in oracle sql without hardcoding -
i took pre-image/post-image of table before , after ran batchjob.
pre_table
id|name |phone |score 1 |john |145678 |10 2 |ptr |23456789 |20 3 |sarah |34567890 |30 4 |mary |45678901 |40
post_table
id|name |phone |score 1 |john |12345678 |10 2 |peter |23456789 |22 3 |sarah |34567890 |33 4 |mary |45678901 |40
how compare , extract modified fields , present them follows:
desired output
id|modifiedcolumn|modifiedval|prevval 1 |phone |12345678 |145678 2 |name |peter |ptr 2 |score |22 |20 3 |score |33 |30
so far, can modified rows using
select * post_table minus select * pre_table
which gives me
id|name |phone |score 1 |john |12345678 |10 2 |peter |23456789 |22 3 |sarah |34567890 |33
i thinking of using all_tab_columns iterate fields in table
select column_name all_tab_columns table_name ='pre_table';
i have written partial pl-sql procedure, got stuck when wanted compare column names.
procedure diff cursor post_cur (select * post_table minus select * pre_table); cursor pre_cur (select * pre_table); cursor columns_cur select column_name all_tab_columns table_name ='post_table'; begin r in post_cur loop p in pre_cur loop if (r.id=p.id) f in columns_cur loop dbms_output.put_line('id:'||r.id||'_'||f.column_name); --compare f.column_name.. stuck here!! --if (r.[f.column_name] <>p.[f.column_name] ) --dbms_output.put_line('id:'||r.id||'_'||f.column_name||'_'||r.[f.column_name]||'_'||p.[f.column_name]); --end if; end loop; end if; end loop; end loop; end diff;
how table results dynamically without hardcoding table column names? open use pl-sql or else works. tia.
i think can results looking using following sql:
--phone select a.id, 'phone' modifiedcolumn, b.phone modifiedval, a.phone prevval pre_table inner join post_table b on a.id = b.id , coalesce(a.phone,'-') <> coalesce(b.phone,'-') --name union select a.id, 'name' modifiedcolumn, b.phone modifiedval, a.phone prevval pre_table inner join post_table b on a.id = b.id , coalesce(a.name,'-') <> coalesce(b.name,'-') --score union select a.id, 'score' modifiedcolumn, b.phone modifiedval, a.phone prevval pre_table inner join post_table b on a.id = b.id , coalesce(a.score,'0') <> coalesce(b.score,'0')
Comments
Post a Comment