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

db2error handlingstored-procedures

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.

Best Answer

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