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);