I would like to write “procedures” in DB2 9.7 without defining the “CREATE PROCEDURE” -statement. Apparently this is something called “compiled” sql statement. However, I am having problems in getting valid syntax. E.g. the syntax below does not seem to work:
BEGIN
DECLARE V_SQL VARCHAR(1024);
SET V_SQL = 'BEGIN
IF EXISTS(SELECT NAME FROM SYSIBM.SYSTRIGGERS WHERE NAME = ''TRIGGER_EMPLOYEE_FOR_DELETES'') THEN
DROP TRIGGER TRIGGER_EMPLOYEE_FOR_DELETES;
END IF;
END;';
PREPARE S1 FROM V_SQL;
EXECUTE S1;
END
I have tried adding/removing “;” and statement symbol “!” but still cannot get it to work.
You cannot have a
DROP TRIGGERstatement within a compound SQL statement. See the DB2 documentation for compound SQL.If you are able to move the
IFstatement outside ofV_SQL, you could do something like this:Of course, this wouldn’t work if you need to set your condition dynamically.