Connecting Tech Pros Worldwide Forums | Help | Site Map

Using Merge command

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#1   May 9 '07
Using Merge command (supported version 9 onwards)
========================================
Used to go for insert/update in a single command based on some condition.

Example #1

Expand|Select|Wrap|Line Numbers
  1. merge into emp5 e
  2. using (select empno,ename from emp) e1
  3. on (e.empno=e1.empno)
  4. when matched then
  5. update set
  6. e.ename=e1.ename
  7.  when not matched then
  8. insert values(e1.empno,e1.ename)
  9.  
If the empno exists then update else insert the record.

Example#2

Expand|Select|Wrap|Line Numbers
  1.  
  2. merge into empls e
  3. using emp e1
  4. on (e.empno=e1.empno)
  5. when matched then
  6. update set
  7. e.ename=e1.ename,
  8. e.job=e1.job,
  9. e.mgr=e1.mgr,
  10. e.hiredate=e1.hiredate,
  11. e.sal=e1.sal,
  12. e.comm=e1.comm,
  13. e.deptno=e1.deptno
  14. when not matched then
  15. insert values(e1.empno,e1.ename,e1.job,e1.mgr,e1.hiredate,e1.sal,e1.comm,e1.deptno)
  16.  

Just another simple example .

Last edited by debasisdas; Feb 11 '08 at 06:29 AM. Reason: added code=oracle tags



Reply


Similar Oracle Database bytes