1

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")
MT0
  • 143,790
  • 11
  • 59
  • 117
Frances
  • 11
  • 1

1 Answers1

1

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);
MT0
  • 143,790
  • 11
  • 59
  • 117
  • As an aside, your use of double-quotes in 'where session_id in ("3232323","23099999","567890111") is an error in itself. Double-quotes are used to create/reference a case-sensitive object name, which is bad practice in oracle but _that_ is a different discussion. Your usage here should be, at best, single-quotes - to indicate a string value. And if the column session_id is actually of data type NUMBER, then you should not use any quotes at all around those numbers. – EdStevens Feb 07 '22 at 19:10
  • @EdStevens I do not use double quotes in the answer. I even commented in the code that they should be single quoted. – MT0 Feb 07 '22 at 19:28
  • @MT0 - my apologies. I meant to make that comment as a reply to the OP. Accidentally hit the wrong 'reply' link. Or is that "reply" link? :-) – EdStevens Feb 08 '22 at 00:07