Insert Statement - Multiple Tables with Where Not Exists
601005Nov 7 2008 — edited Nov 10 2008Hello,
Im trying to put together an insert statement and I'd like to insert 1 record into each of the three tables (main, hist, and ref) at once. The three tables have constraints so it will need to check the tables on the fly to see if the data already exists, if it does not then execute the insert. Here is what I have so far... I really don't want to create a function, so would rather stick with SQL for now. Is the where not exists statement the best option? Ive been unable to get this statement to work without error. Any input will be appreciated, thanks ahead of time!
INSERT ALL
into main (CUST_NM,MCN,APN,DNIS)
values ('BAD COMPANY','999A999','999999','0000') where not exists (SELECT apn
from main
where apn = '999999'
and dnis = '0000')
into hist (TRANS_DT,
TRANS_TYPE,
UID,
EMAIL,
PHONE,
CUST_NM,
MCN,
APN,
DNIS)
values (sysdate,
'IL',
'BADFROG',
'VERY@BADFROG.COM',
'1112223333',
'BAD COMPANY',
'999A999',
'999999',
'0000') where not exists (SELECT apn
from hist
where apn = '999999'
and dnis = '0000')
into ref values ('999999') where not exists (SELECT apn
from ref
where apn = '999999'
and dnis = '0000')
SELECT * from dual;
Edited by: AllRightyRighterMan on Nov 7, 2008 1:06 PM
Edited by: AllRightyRighterMan on Nov 7, 2008 1:24 PM