Question

I would like to insert records in SQL such that if the combination of entry exists then the script should not proceed with the insert statement. here is what i have so far :

insert into TABLE_TESTING(R_COMPONENT_ID,OPRID) 
select 1942,'Test' 
from  TABLE_TESTING 
where not exists 
    (select * 
     from TABLE_TESTING 
     where R_COMPONENT_ID='1942' 
       and oprid ='Test');

I have a table name as : TABLE_TESTING It has two columns as : R_COMPONENT_ID and OPRID

If the combination of record as '1942' and 'Test' already exist in DB then my script should not perform insert operation and if it doesent exists then it should insert the record as a combination of R_COMPONENT_ID and OPRID.

Please suggest. Using the query specified above i am getting multiple insert been added in the DB. Please suggest some solution.

Was it helpful?

Solution 3

Here is an skelton to use MERGE. I ran it and it works fine. You may tweak it further per your needs. Hope this helps!

DECLARE
BEGIN
   FOR CURTESTING IN (SELECT R_COMPONENT_ID, OPRID FROM TABLE_TESTING)
   LOOP
      MERGE INTO TABLE_TESTING
           USING DUAL
              ON (R_COMPONENT_ID = '1942' AND OPRID = 'Test')
      WHEN NOT MATCHED
      THEN
         INSERT     (PK, R_COMPONENT_ID, OPRID)
             VALUES (TEST_TABLE.NEXTVAL, '1942', 'Test');
   END LOOP;

   COMMIT;
END;

OTHER TIPS

As you don't want to update existing rows, your approach is essentially correct. The only change you have to do, is to replace the from table_testing in the source of the insert statement:

insert into TABLE_TESTING (R_COMPONENT_ID,OPRID) 
select 1942,'Test' 
from  dual -- <<< this is the change
where not exists 
    (select * 
     from TABLE_TESTING 
     where R_COMPONENT_ID = 1942
       and oprid = 'Test');

When you use from table_testing this means that the insert tries to insert one row for each row in TABLE_TESTING. But you only want to insert a single row. Selecting from DUAL will achieve exactly that.

As others have pointed out, you can also use the MERGE statement for this which might be a bit better if you need to insert more than just a single row.

merge into table_testing target
using 
(
    select 1942 as R_COMPONENT_ID, 'Test' as OPRID from dual
    union all 
    select 1943, 'Test2' from dual
) src
ON (src.r_component_id = target.r_component_id and src.oprid = target.oprid)
when not matched
then insert (r_component_id, oprid)
     values (src.r_component_id, src.oprid);

Try This one

if not exists(Select * From TABLE_TESTING where R_COMPONENT_ID='1942' and OPRID='Test' )
begin
    insert into TABLE_TESTING(R_COMPONENT_ID,OPRID) values('1942','Test')
end

You can make use of MERGE operation.

insert into TABLE_TESTING  
select 1942,'Test' where 0<(
select count(1) from TABLE_TESTING 
where not exists(select 1 from TABLE_TESTING  where R_COMPONENT_ID=1942 and oprid ='Test'))

Try the above code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top