Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Merge - Change Capture and Print

Hi Guru,
Is there a method to print the values of insert, update, and remove before and after a MERGE statement has been executed?
If so, could you kindly give one example;
Thanks
Answers
-
What is "the value of insert"? Also, what is "remove"? Do you mean "delete"? That is a very basic term in SQL; if you mean "delete", then say "delete".
It would help if you gave an example: the table before MERGE, then the MERGE statement (no need to tell us what the table looks like after MERGE - we can figure that out), and then the exact output you want from your question in this thread. What do you need to report? For example: Does your table have a primary key, and perhaps, as part of the requirement, you need to say which primary keys had changes? Which were deleted, which were added (via insert), and which had data updated? What else? What is the exact format of what you need?
-
create table t1(n1 number, n2 number); insert into t1 select level, level*100 from dual connect by level<=3; commit; <<ab>> declare lt varchar2(100 char); ltr raw(16); scn number; type tr is record (op varchar2(1 char), n1 number, n2 number, n2_old number); begin lock table t1 in share mode; lt := dbms_transaction.local_transaction_id(true); select t.xid, start_scn into ltr, scn from v$transaction t where t.xidusn||'.'||t.xidslot||'.'||t.xidsqn = ab.lt; -- merge into t1 t using ( select 2 n1, 222 n2 from dual union all select 3, null from dual union all select 4, 444 from dual) s on (t.n1 = s.n1) when not matched then insert (n1, n2) values(s.n1, s.n2) when matched then update set n2 = s.n2 delete where s.n2 is null; commit; -- for c tr in (execute immediate ' select versions_operation op, t.n1, t.n2, t2.n2 n2_old from t1 versions between scn minvalue and maxvalue t left join t1 as of scn ('||scn||') t2 on t2.n1 = t.n1 where t.versions_xid = :ltr' using ltr) loop dbms_output.put_line('op='||c.op||' n1='||c.n1||' n2='||c.n2_old||'->'||case when c.op='D' then '' else c.n2 end); end loop; end; / op=U n1=2 n2=200->222 op=D n1=3 n2=300-> op=I n1=4 n2=->444 select * from t1; N1 N2 ---------- ---------- 4 444 1 100 2 222