Home > OS >  DB2: How to ignore exceptions?
DB2: How to ignore exceptions?

Time:06-16

i need to add a merge into my batch, but in case of errors it will get ignored and the batch will just keep going. I know that if i do something like this:

BEGIN
 DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'
BEGIN END;
 EXECUTE IMMEDIATE 'DROP TABLE test.test1';
END

it will work, but right now i need a larger command, not that single liner and also i never understood the EXECUTE IMMEDIATE.

In Oracle i guess i could just do

begin
 my code
exception
 when others then
 null;
end;

and ignore even any kind of exception, is there anything similar in db2?

CodePudding user response:

A handler declared in a BEGIN END block as below swallows all exceptions in the same block:

BEGIN
 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
 EXECUTE IMMEDIATE 'DROP TABLE test.test1';
 EXECUTE IMMEDIATE 'CREATE TABLE MYTAB (I INT)';
 EXECUTE IMMEDIATE 'DROP TABLE test.test1';
 EXECUTE IMMEDIATE 'INSERT INTO MYTAB (I) VALUES 1, 2, 3';
END

dbfiddle link.

  • Related