我可以暂时禁用 oracle 存储过程中的触发器吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7332058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I temporarily disable a trigger in an oracle stored procedure?
提问by Giuseppe
Can I temporarily disable a trigger in an oracle stored procedure?
我可以暂时禁用 oracle 存储过程中的触发器吗?
example (pseudocode):
示例(伪代码):
MyProcedure{
disable MyTrigger;
//doStuff
enable MyTrigger;
};
thx in advance. Giuseppe
提前谢谢。朱塞佩
回答by Ollie
You can issue DDL such as "ALTER TRIGGER" statements via dynamic SQL using the EXECUTE IMMEDIATE syntax.
您可以使用 EXECUTE IMMEDIATE 语法通过动态 SQL 发出 DDL,例如“ALTER TRIGGER”语句。
A description of that is here: http://download.oracle.com/docs/cd/B12037_01/appdev.101/b10807/13_elems017.htm
对此的描述在这里:http: //download.oracle.com/docs/cd/B12037_01/appdev.101/b10807/13_elems017.htm
PROCEDURE myProcedure
IS
BEGIN
EXECUTE IMMEDIATE 'ALTER TRIGGER triggername DISABLE';
-- Do work
EXECUTE IMMEDIATE 'ALTER TRIGGER triggername ENABLE';
EXCEPTION
WHEN OTHERS
THEN
-- Handle Exceptions
END myProcedure;
You can build the dynamic SQL using a VARCHAR variable too if you like:
如果您愿意,也可以使用 VARCHAR 变量构建动态 SQL:
PROCEDURE myProcedure
IS
v_triggername VARCHAR2(30) := 'triggername';
BEGIN
EXECUTE IMMEDIATE 'ALTER TRIGGER '||v_triggername||' DISABLE';
-- Do work
EXECUTE IMMEDIATE 'ALTER TRIGGER '||v_triggername||' ENABLE';
EXCEPTION
WHEN OTHERS
THEN
-- Handle Exceptions
END myProcedure;
If you do this then you should also look into the package DBMS_ASSERT to wrap the triggername and help harden your code against SQL injection attacks.
如果您这样做,那么您还应该查看包 DBMS_ASSERT 来包装触发器名称并帮助加强您的代码以抵御 SQL 注入攻击。