procedure check( value1 char, value2 char)
is
begin
insert into table values (value1, value2 ) where not exists
( select * from table where col1 = value1 and col2 = value2 );
end;
Main Topics
Browse All TopicsDear experts,
I have a case like this:
I have a table with column1 and column2. I need to pass a value "C" entered by users in to the function to compare with all the data in column1. It the "C" exists in the column1, then I need to compare another value "D" passed into the function with column2. If "D" exist in column2, then don't do anything, otherwise, insert "C","D" into this table.
If "C" does not exist in column1, then I need to insert "C" and "D" into the table.
How to write a procedure like this? thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I though about it last night and what I gave you was wrong.
the code
declare
select count(column1) into isfound1 from tablename where column1 = letter1;
select count(column2) into isfound2 from tablename where column2 = letter2;
--------------------------
should be
--------------------------
declare
select count(column1) into isfound1 from tablename where column1 = letter1;
select count(column2) into isfound2 from tablename where column1 = letter1 and column2 = letter2 ;
sorry
create unique index tab_uk1 on table (col1,col2);
procedure check( value1 char, value2 char)
is
begin
begin
insert into table values (value1, value2 );
exception
where others null;
end;
end;
Even better would be to declare (col1,col2) to be the primary key. But there is a little difference: UKs allow for nulls and regards (null,null) and (null,null) as being different and (1,null) and (1, null) as being equal...
I know this is no good style to "program via exception", but creating constraints to enforce business rules is. Here is my prefered solution:
procedure check( value1 char, value2 char)
is
begin
insert into table
select value1, value2
from dual where not exists (
select 1 from table where col1=value1 and col2=value2 );
end;
This uses the index created above, but does not handle null values properly!
@chetankr: This is what you meant, right? The statement you gave didn't work (Oracle 9.2)...
Business Accounts
Answer for Membership
by: razlinPosted on 2003-08-27 at 19:16:39ID: 9237205
procedure check(letter1 char,letter2 char) is
begin
isfound1 number;
isfound2 number;
declare
select count(column1) into isfound1 from tablename where column1 = letter1;
select count(column2) into isfound2 from tablename where column2 = letter2;
begin
-- if first one is not found add it in
if (isfound1 = 0) then
insert into tablename (column1,column2) values (letter1.letter2);
else
-- if the first one is found but the second one is not found
if (isfound2 = 0) then
insert into tablename (column1,column2) values (letter1.letter2);
end if;
end if;
end;
end;
sorry for the indenting but tab brought me to the submit button.
hope this helps.