How to Insert records only where records do not exists in table to avoid duplicates Oracle

Viewed 35

Hello please see script below i would love to edit the script so it inserts into the table "Book" certain records coming from table "Book2". In other to avoid duplicates , I want it to insert the records into Book only where the records do not exists in the table Book. Database is Oracle

insert into mol.bkp_Book
select * from mol.bkp_Book2
where session_id in ("3232323","23099999","567890111")
1 Answers

Use a MERGE statement:

MERGE INTO mol.bkp_Book dst
USING (
  select *
  from   mol.bkp_Book2
  where  session_id in ('3232323','23099999','567890111') -- Single quotes
) src
ON (
    dst.session_id = src.session_id
--AND dst.col1       = src.col1 -- Uncomment if you want to compare all columns
--AND dst.col2       = src.col2
--AND dst.col3       = src.col3
)
WHEN NOT MATCHED THEN
  INSERT (session_id, col1, col2, col3)
  VALUES (src.session_id, src.col1, src.col2, src.col3);
Related