触发并禁用“受影响的行”信息
我有一个触发器类型,而不是插入、更新。 它看起来如下所示:
IF EXISTS(some select staement)
updade
set
where
ELSE IF (some other select staement)
insert
values
ELSE--(3)
RAISERROR ('msg',16,1)
RETURN; SET NOCOUNT ON;
问题是,在第三个“其他”选项中,我想仅显示错误消息,而不显示任何“受影响的行”消息。 SET NOCOUNT ON 对我不起作用。我已经尝试过不同的配置,将其带回和不带回车。我在声明中到处都提到了它。 它在任何地方都不起作用。我使用 SQL Server 2005 有人可以帮助我吗? 受按摩影响的行应始终出现。唯一的例外是 else 语句。
I have a trigger type INSTEAD OF Insert, Update.
It looks something like below:
IF EXISTS(some select staement)
updade
set
where
ELSE IF (some other select staement)
insert
values
ELSE--(3)
RAISERROR ('msg',16,1)
RETURN; SET NOCOUNT ON;
The issue is that in 3rd - "else" option I would like to show only error message without any "row(s) affected" message.
SET NOCOUNT ON dosen't work for me. I've already tried different configurations, put this with and without return. I was putted it everywhere in my statement.
It doesn't work anywhere. I use SQL Server 2005 Can anybody help me please?
Rows affected massage should appear always. The only exception is else statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在查询之前使用
SET NOCOUNT ON;
,然后使用GO
Use
SET NOCOUNT ON;
before a query and then useGO
在一个简化的示例中:
我们得到输出:
抑制“1 行受影响”消息的唯一方法是回滚事务(通过在错误消息后包含 ROLLBACK)。这将产生这样的结果:
没有办法进一步抑制这些消息。 “1 行受影响”消息是在外部作用域(运行 INSERT 语句的作用域)中生成的,而不是在触发器的作用域内生成的,并且生成该消息是因为触发器正在运行至完成 - 到目前为止从外面的说法来看,它已经成功了。
关于 NOCOUNT 的通常建议是使用它来抑制从触发器内返回的额外行计数。如果您为此使用它,它应该是触发器主体中的第一个语句。在您的示例中,您将其作为最后一个语句,无论如何它都不会产生任何影响。
In a simplified example:
We get the output:
The only way to suppress that "1 row affected" message is to rollback the transaction (by including ROLLBACK after the error message). And that will generate this instead:
There's no way to suppress these messages further. The "1 row affected" message is being generated in the outer scope (the scope in which the INSERT statement is being run) rather than within the scope of the trigger, and is being generated because the trigger is running to completion - so far as the outer statement is concerned, it's been succesful.
The usual advice about NOCOUNT is to use it to suppress additional rowcounts being returned from within a trigger. If you're using it for that, it should be the first statement within the body of the trigger. IN your sample, you have it as the last statement, where it will have no effect anyway.
SeeSnapshot
并输出:
并且记录未插入表
SeeSnapshot
and output:
and record not inserted into table