步骤tr_EMPLOYEE2_FORINSERT,第10行插入错误:提供的值的列名或数量与表定义不匹配
问题描述:
ALTER TRIGGER tr_EMPLOYEE2_FORINSERT
ON EMPLOYEE2
FOR INSERT
AS
BEGIN
-- SELECT * FROM INSERTED --INSERTED Table is a special table created for the purposes of Triggers, it is available only in the context of the trigger.
DECLARE @ID INT
SELECT @ID = ID FROM INSERTED
INSERT INTO EMPAUDIT
VALUES('New Employee with id = ' + cast(@id as nvarchar(5)) + ' is added at ' + cast(getdate() as nvarchar(20)))
END
答
a。我们不知道你的EMPAUDIT
表是什么样子。但是,假定它拥有超过一列(大多数表做),你应该使用列单供INSERT
声明:
INSERT INTO EMPAUDIT (Column_To_Insert_Into)
VALUES('New Employee with id = ' + cast(@id as nvarchar(5)) +
' is added at ' + cast(getdate() as nvarchar(20)))
湾但是,触发器实际上仍然断开。为什么?因为inserted
可以包含多行行(或无行)。所以其实我们要的是:
INSERT INTO EMPAUDIT (Column_To_Insert_Into)
SELECT 'New Employee with id = ' + cast(i.id as nvarchar(5)) +
' is added at ' + cast(getdate() as nvarchar(20))
FROM inserted i
,然后你不需要你写在你的触发代码的其余部分