SQL Server 更新触发器(非唯一插入问题)
我有一个超级简单的表,看起来像这样:
CREATE TABLE [dbo].[TestTable](
[SomeColumn] [int] NOT NULL )
我在另一个表上也有一个超级简单的触发器,看起来像这样:
ALTER TRIGGER [dbo].[trg_Audit_TableXYZ] ON [dbo].[TableXYZ] AFTER UPDATE
AS
INSERT INTO [dbo].[TestTable] Values (123)
我的问题是,当触发器运行时,我收到以下错误:
行更新或删除的值要么不会使该行唯一,要么会更改多行(2 行)。
我不明白,为什么会收到此错误?
谢谢。
I have a super simple table that looks something like this:
CREATE TABLE [dbo].[TestTable](
[SomeColumn] [int] NOT NULL )
I also have a super simple trigger on another table that looks something like this:
ALTER TRIGGER [dbo].[trg_Audit_TableXYZ] ON [dbo].[TableXYZ] AFTER UPDATE
AS
INSERT INTO [dbo].[TestTable] Values (123)
My problem is that when the trigger runs I get the following error:
The row value(s) updated or deleted either do not make the row unique or they alter multiple rows (2 rows).
I don't get it, why would I get this error?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
SET NOCOUNT ON
添加到触发器定义的顶部。这将抑制触发器发出的影响消息的其他行并混淆 SSMS。IE
Add
SET NOCOUNT ON
to the top of the trigger definition. This will suppress the additional rows affected message that emanates from the trigger and confuses SSMS.i.e.
我无法重新创建。这是否与其他触发器或约束或其他东西相冲突?我不知道。
更新:
正如 Mikael 所说,在 TableXYZ 中添加主键将解决该问题。仅当您使用 SSMS 修改表时才会发生。谢谢米凯尔。这有效:
I can't recreate. Is this conflicting with some other trigger or a constraint or something, maybe? I don't know.
Update:
As Mikael said, adding a primary key in TableXYZ will work-around the issue. Only happens when you are modifying the table with SSMS. Thanks Mikael. This works: