编写IBM DB2 9.7内联或编译的sql语句不起作用

时间:2012-08-20 10:12:52

标签: triggers db2 procedures

我想在DB2 9.7中编写“过程”而不定义“CREATE PROCEDURE”-statement。显然这是一个叫做“编译”的sql语句。但是,我在获取有效语法方面遇到了问题。例如。下面的语法似乎不起作用:

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

我尝试过添加/删除“;”和陈述符号“!”但仍然无法让它发挥作用。

1 个答案:

答案 0 :(得分:1)

在复合SQL语句中不能有DROP TRIGGER语句。请参阅DB2 documentation for compound SQL

如果您能够将IF语句移到V_SQL之外,您可以执行以下操作:

BEGIN
    DECLARE V_SQL VARCHAR(1024);
    IF EXISTS(SELECT NAME FROM SYSIBM.SYSTRIGGERS 
                  WHERE NAME = 'TRIGGER_EMPLOYEE_FOR_DELETES'
    ) THEN
        SET V_SQL = 'DROP TRIGGER TRIGGER_EMPLOYEE_FOR_DELETES;';
        PREPARE S1 FROM V_SQL;
        EXECUTE S1;
    END IF;
END

当然,如果您需要动态设置条件,这将不起作用。