触发器返回结果集,并且服务器选项“禁止来自触发器的结果”是真的
我有一个针对不返回结果集的表运行的触发器:
ALTER TRIGGER [dbo].[LogDelete_Nodes] ON [dbo].[Nodes]
FOR DELETE
AS
BEGIN
SET NOCOUNT ON;
/* Load the saved context info UserGUID */
DECLARE @SavedUserGUID uniqueidentifier
SET @SavedUserGUID = (SELECT CAST(context_info as uniqueidentifier)
FROM master.dbo.sysprocesses
WHERE spid = @@SPID)
--remaining trigger contents deleted to provide simple example
END;
从 Nodes
表中删除行将导致错误:
消息 524,级别 16,状态 1,过程 LogDelete_Nodes,第 10 行
触发器返回结果集,并且服务器选项“不允许来自触发器的结果”为 true。
如何使不返回结果集的触发器不返回结果集?
注意: 触发器最初定义为使用未弃用的语法来设置变量:
SELECT @SavedUserGUID = CAST(context_info as uniqueidentifier)
FROM master.dbo.sysprocesses
WHERE spid = @@SPID
但它仍然给出相同的错误。
SELECT @@version
Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) Nov 24 2008 13:01:59
Copyright (C) 1988-2005 Microsoft Corporation
Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
答案如下:
当我撰写这个问题时,我们就解决了这个问题。我仍然会问这个问题,因为事实证明答案对于其他遇到这个神秘问题的人来说非常有帮助。
另请参阅
i have a trigger running against a table that does not return a results set:
ALTER TRIGGER [dbo].[LogDelete_Nodes] ON [dbo].[Nodes]
FOR DELETE
AS
BEGIN
SET NOCOUNT ON;
/* Load the saved context info UserGUID */
DECLARE @SavedUserGUID uniqueidentifier
SET @SavedUserGUID = (SELECT CAST(context_info as uniqueidentifier)
FROM master.dbo.sysprocesses
WHERE spid = @@SPID)
--remaining trigger contents deleted to provide simple example
END;
Deleting rows from the Nodes
table will cause the error:
Msg 524, Level 16, State 1, Procedure LogDelete_Nodes, Line 10
A trigger returned a resultset and the server option 'disallow results from triggers' is true.
How do i make a trigger that does not return a resultset not return a resultset?
Note: The trigger was originally defined to use the non-deprecated syntax for setting a variable:
SELECT @SavedUserGUID = CAST(context_info as uniqueidentifier)
FROM master.dbo.sysprocesses
WHERE spid = @@SPID
and it still gives the same error.
SELECT @@version
Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) Nov 24 2008 13:01:59
Copyright (C) 1988-2005 Microsoft Corporation
Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
Answer to follow
We figured out the problem while i was composing this question. i'm still going to ask the question, since the answer can prove to be very helpful to anyone else having this mysterious problem.
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题的解决方案原来是因为启用了 SSMS 选项包括实际执行计划。
SQL Server(错误地)计算了触发器返回的包含执行计划信息的“秘密”结果集。
关闭查看执行计划的选项,就可以了。这使得跟踪执行计划变得困难,因为您不允许查看执行计划。
这很可能是 SQL Server 2005 中的一个错误。
The solution to the problem turned out to be because the SSMS option Include Actual Execution Plan was enabled.
SQL Server is (mistakenly) counting the "secret" resultset containing execution plan information as being returned by the trigger.
Turn off the option to see the execution plan, and it works. This makes tracing execution plans difficult, since you're not allowed to see execution plans.
This is most likely a bug in SQL Server 2005.