I’m trying to insert a single row which may or may not already exist. I’d like to avoid selecting it first and/or getting a -803 if it does exist. I’ve done some research and tried both ignore and the merge statement, but keep getting syntax errors on both. In any case, I’m not trying copy the data from another table – so, merge is not really appropriate.
Isn’t there some way in DB2 SQL to just issue a failure-proof insert and not have to code around this? In other words, is there some insert syntax which guarantees the data will be added if it doesn’t exist or will return zero status even it it does?
I’m trying to insert a single row which may or may not already exist.
Share
In short, the answer is no. In long, it all depends on the constraints you have set up on your table. If you attempt to execute an insert against a table that has a unique constraint on a column, and the data already exists that you are trying to insert, you will get an error in DB2 (and any other RDBMS).
In this case, your best option is probably to write a stored procedure that checks to see if the record already exists before creating it.