Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Found or Not found ?

324137Dec 5 2006 — edited Dec 5 2006
Hello all,

What is the best way, in a PL/SQL program, to check for the existence of a record and create it if it doesn't exist.

There are those 2 ways that I know of:

1) Using the SELECT / EXCEPTION

SELECT <col> INTO <var> FROM table WHERE <onerecordcondition>;
:
:

EXCEPTION
WHEN_NO_DATA_FOUND

I hate that way which breaks the program flow and you have to place BEGIN/EXCEPTION/END statements everywhere where you need to check that condition for different tables. I prefer to check the value of %NOTFOUND right after the statement but that doesn't work with SELECT INTO.

2) Using UPDATE / %NOTFOUND

UPDATE <table> SET <col> = <samecol> WHERE <onerecordcondition>;
IF SQL%NOTFOUND THEN
:
:
End IF;

This keeps the program flow and doesn't require BEGIN/END blocks but it performs an unecessary update to the table.

Is there any other better way to perform such an easy task in PL/SQL ?

Thank you.

p.s. Oracle 9i database.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 2 2007
Added on Dec 5 2006
10 comments
562 views