Oracle - Avoid Inserting Duplicate Rows

Contents

Avoid duplicates insert and select


 INSERT INTO DEPARTMENT
 (
     ID
    ,DESC
    ,CRT
 )
 SELECT 
    20 as ID ,Human Resources' as DESC, TO_DATE('15.11.2008', 'DD.MM.YYYY') as CRT 
 FROM DUAL WHERE NOT EXISTS (SELECT * FROM DEPARTMENT WHERE DESC = 'Human Resources');

Avoid duplicates using Oracle merge


 MERGE INTO DEPARTMENT TRG
 USING (
    select 73 as ID ,'Sales' as DESC, TO_DATE('19.11.2008', 'DD.MM.YYYY') as CRT  from dual  
  ) SRC
 ON ( SRC.DESC = TRG.DESC)
 WHEN NOT MATCHED THEN
     INSERT (
           ID
         , DESC
         , CRT
 ) VALUES (
           SRC.ID
         , SRC.DESC
         , SRC.CRT
 )
 WHEN MATCHED THEN
     UPDATE SET TRG.ID = SRC.ID , TRG.CRT = SRC.CRT
 ;


See Also:
Oracle-Merge-Statement
Status: Published Date: 2017/06/05 14:14:14 Revision: 1.1

Copyright bei Andreas Haack (C) 2014. Diese Seite wird so wie sie ist zur Verfuegung gestellt, ohne irgenweche Garantien der Verwendbarkeit fuer bestimte Zwecke. Die auf dieser Seiten angebrachten Links liegen ausserhalb der redaktionellen Verantwortung von Andreas Haack und es wird keine Haftung oder Garantie uebernommen. Die Seiten sind Copyright (c) 2014 von Andreas Haack. Kein Teil darf ohne die schriftliche Einverstaendnis von Andreas Haack veroeffentlicht werden.
The page is provided 'as is' , without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fit- ness for a particular purpose and non-infringement. In no event shall Andreas Haack be liable for any claim, damages or other liability. This page is copyrighted property of Andreas Haack. Copyright by Andreas Haack (c) 2014 . No part of this page may be published without written permission for Andreas Haack. A hyper-link may created to this page but NOT to the embedded elements of this page. It may be freely downloaded for private purpose only as long as it is unaltered.