HiveBrain v1.2.0
Get Started
← Back to all entries
debugMinor

How to ignore sql errors in stored procedure ? (Not handle)

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
ignorestoredhandlesqlprocedurehowerrorsnot

Problem

I have a list of records I want to process and the cursor iterates through them from a table. The procedure just exits when it hits an insert statement and it fails. Is there a way I can ignore the errors and continue processing the following records ?

Cursor C1:
for each record:
insert into table_t1(col1) values ('...'); <-- Insert statement fails for record 'n'
End cursor

I would like for it to not exit out but continue processing from n+1 to the end of cursor.

Any help appreciated.

Solution

Found it.

You have to declare a continue handler instead of an exit handler. That way even if there is an exception, it goes into the handler and continues with the next record.


DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING



BEGIN
...
Code that handles the exception/warning
...
END

Context

StackExchange Database Administrators Q#88862, answer score: 5

Revisions (0)

No revisions yet.