我有一个表,允许在我在该表中创建几个新列之前进入。该表调用UPDATE,DELETE和INSERT上的触发器,它在我使用History
表中的相同列创建的日志历史记录表中插入数据。
现在不允许我在其显示的表格中插入数据。当我从编辑选项中编辑行时显示此错误.....
No row was Updated.
The data in row 1 was not committed
Error Sources .net SQLClient Data Provider.
Error Message: Incorrect syntax near the keyword where
Correct the error and retry or press esc to cancel
但如果我禁用触发器,一切正常。
触发
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [magic ].[magic ]
ON magic
AFTER UPDATE, INSERT, DELETE
AS
DECLARE @operation CHAR(20)
SET @operation= 'N'
IF EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted)
BEGIN
SET @operation = 'Updated'
INSERT INTO magic.magic_HISTORY
SELECT *, @operation
FROM INSERTED
END
IF EXISTS(SELECT * FROM inserted) AND NOT EXISTS(SELECT * FROM deleted)
BEGIN
SET @operation = 'Inserted'
INSERT INTO magic.magic_HISTORY
SELECT *, @operation
FROM INSERTED
END
IF NOT EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted)
BEGIN
SET @operation = 'Deleted'
INSERT INTO magic.magic_HISTORY
SELECT *, @operation
FROM deleted
END
答案 0 :(得分:1)
如果这是我的触发器,我会更改逻辑以使其更简单。首先,审计插入的值没有意义。你拥有魔术表中的所有值,因此拥有它的第二个副本是多余的。这使得审计更简单。您只需要担心之前表中的值。
ALTER TRIGGER [magic ].[magic ] on magic
AFTER UPDATE, DELETE AS
Declare @operation char(20) SET @operation= 'N'
IF EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted)
SET @operation = 'Updated'
IF NOT EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted)
SET @operation = 'Deleted'
INSERT INTO magic.magic_HISTORY
SELECT *,@operation FROM deleted
答案 1 :(得分:0)
当您向magic.magic_HISTORY添加列时,您运行了:
ALTER TABLE magic.magic_HISTORY ADD column
如果是,那么改变:
INSERT INTO magic.magic_HISTORY
SELECT *,@operation FROM
到
INSERT INTO magic.magic_HISTORY (column list)
SELECT *,@operation FROM
答案 2 :(得分:0)
您使用什么工具/应用程序来插入/更新到表中? 您可以使用SQLProfiler捕获发送到数据库的特定SQL。也许在包装SQL中有一个语法错误,它会调用触发器。
另外,尝试在插入历史记录之前添加到触发器set nocount on
,然后在set nocount off
之后添加。这将减少驱动程序被来自触发器的“受影响行”消息混淆的可能性。