是否可以传递参数以在 SQL Server 数据库中触发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3883613/
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
Is it possible to pass a parameter to trigger in SQL Server database?
提问by KentZhou
Table can have trigger on insert/delete/update. Trigger will be fired internally by DB engine.
表可以在插入/删除/更新时触发。触发器将由数据库引擎在内部触发。
Is it possible to pass a parameter to trigger in SQL Server database like a stored procedure?
是否可以像存储过程一样传递参数以在 SQL Server 数据库中触发?
回答by Remus Rusanu
Indirectly via CONTEXT_INFO()
. See Using Session Context Information. The fact that your trigger needs extra state is always a code smell.
间接通过CONTEXT_INFO()
。请参阅使用会话上下文信息。您的触发器需要额外状态的事实始终是代码异味。
回答by Brian Driscoll
回答by JeremyDWill
You can kludge it. Create a table specifically designed to hold the parameter information, then place data into the the parameter table before performing the operation that will initiate the trigger. Code the trigger to read from the parameter table.
你可以拼凑它。创建一个专门用于保存参数信息的表,然后在执行将启动触发器的操作之前将数据放入参数表中。编码触发器以从参数表中读取。
That was the carrot, here is the stick:
那是胡萝卜,这是大棒:
- This will complicate your triggers. You will need to account for the potential of missing paramters, and you will have to provide cleanup for the parameters.
- You are creating a dependency with this technique, so it will only work with processes that are coded to use this parameter passing technique. So if your data is modified by more than just your application, this won't work.
- 这将使您的触发器复杂化。您需要考虑丢失参数的可能性,并且必须为参数提供清理。
- 您正在使用此技术创建依赖项,因此它仅适用于编码为使用此参数传递技术的进程。因此,如果您的数据被不仅仅是您的应用程序修改,这将不起作用。