Skip to content
Advertisement

Check if record exists, and find out which column(s) has changed

I have a procedure that has the following code:

This works fine on an insert. But if I try to update an existing record, I get an exception saying: ORA-00001: unique constraint on the primary key, which is the ID.

I figured I have to make a check if the record already exists, then make an update on the specified column. But the table has like 30 columns or more, and while writing the sql code, I don’t know which column the user is trying to alter. How can I do this. Say there is a column called NAME, and I know that’s the column the user is trying to change, then I can do something like:

And I need to merge the IF/WHEN EXISTS with the INSERT, but I’m a little lost, as I’m still new to SQL.

PS. The cur_ID is a parameter give to the procedure, and it comes from the user (an aCREATE OR REPLACE PROCEDURE "spUpdateDonRecipRelation"(cur_ID IN NUMBER)

Advertisement

Answer

First, you can still get duplicates from the select. The distinct does not prevent this. I would suggest:

Then, you can put the check in a WHERE clause:

(I am guessing that cur_id comes from the don table.)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement