触发并禁用“受影响的行”信息

发布于 2024-10-11 18:33:53 字数 439 浏览 1 评论 0原文

我有一个触发器类型,而不是插入、更新。 它看起来如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半枫 2024-10-18 18:33:53

在查询之前使用 SET NOCOUNT ON;,然后使用 GO

SET NOCOUNT ON;
GO

Use SET NOCOUNT ON; before a query and then use GO

SET NOCOUNT ON;
GO
栩栩如生 2024-10-18 18:33:53

在一个简化的示例中:

create table T (
    ID int not null
)
go
create trigger TT
on T
instead of insert
as
    RAISERROR('No',16,1)
go
insert into T (ID)
select 1

我们得到输出:

Msg 50000, Level 16, State 1, Procedure TT, Line 5
No

(1 row(s) affected)

抑制“1 行受影响”消息的唯一方法是回滚事务(通过在错误消息后包含 ROLLBACK)。这将产生这样的结果:

Msg 50000, Level 16, State 1, Procedure TT, Line 5
No
Msg 3609, Level 16, State 1, Line 1
The transaction ended in the trigger. The batch has been aborted.

没有办法进一步抑制这些消息。 “1 行受影响”消息是在外部作用域(运行 INSERT 语句的作用域)中生成的,而不是在触发器的作用域内生成的,并且生成该消息是因为触发器正在运行至完成 - 到目前为止从外面的说法来看,它已经成功了。

关于 NOCOUNT 的通常建议是使用它来抑制从触发器内返回的额外行计数。如果您为此使用它,它应该是触发器主体中的第一个语句。在您的示例中,您将其作为最后一个语句,无论如何它都不会产生任何影响。

In a simplified example:

create table T (
    ID int not null
)
go
create trigger TT
on T
instead of insert
as
    RAISERROR('No',16,1)
go
insert into T (ID)
select 1

We get the output:

Msg 50000, Level 16, State 1, Procedure TT, Line 5
No

(1 row(s) affected)

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:

Msg 50000, Level 16, State 1, Procedure TT, Line 5
No
Msg 3609, Level 16, State 1, Line 1
The transaction ended in the trigger. The batch has been aborted.

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.

缱绻入梦 2024-10-18 18:33:53

SeeSnapshot

1

create TRIGGER triggerdeletefordesignation 
   ON  designation 
   instead of INSERT,DELETE,update
AS 
BEGIN
    print '"operation on this table is not allowed"'
    rollback
END
GO

insert into  designation values(6,'lead');
exec getDesg

并输出:

    "operation on this table is not allowed"
Msg 3609, Level 16, State 1, Line 12
The transaction ended in the trigger. The batch has been aborted.

并且记录未插入表

SeeSnapshot

1

create TRIGGER triggerdeletefordesignation 
   ON  designation 
   instead of INSERT,DELETE,update
AS 
BEGIN
    print '"operation on this table is not allowed"'
    rollback
END
GO

insert into  designation values(6,'lead');
exec getDesg

and output:

    "operation on this table is not allowed"
Msg 3609, Level 16, State 1, Line 12
The transaction ended in the trigger. The batch has been aborted.

and record not inserted into table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文