一般我们在触发器中都会使用@@ROWCOUNT和SET NOCOUNT ON。但是如果不注意它们之间的顺序则可能出问题。今天写了一个触发器一直不起作用,找了很久一直没有找到原因。 代码如下:
ALTER TRIGGER [trg_Houses_Updated]
ON [dbo].[Houses]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF @@ROWCOUNT = 0
RETURN;
...
END
ON [dbo].[Houses]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF @@ROWCOUNT = 0
RETURN;
...
END
最后突然想起来可能是SET NOCOUNT ON的原因,使@@RowCount不起作用了。然后尝试的将SET NOCOUNT ON和@@RowCount的前后顺序条换了一下果然可以使用了。修改后的代码如下:
1 ALTER TRIGGER [trg_Houses_Updated]
2 ON [dbo].[Houses]
3 AFTER UPDATE
4 AS
5 BEGIN
6 IF @@ROWCOUNT = 0
7 RETURN;
8
9 SET NOCOUNT ON;
10 ...
11 END
2 ON [dbo].[Houses]
3 AFTER UPDATE
4 AS
5 BEGIN
6 IF @@ROWCOUNT = 0
7 RETURN;
8
9 SET NOCOUNT ON;
10 ...
11 END