【问题标题】:Procedure tr_EMPLOYEE2_FORINSERT, Line 10 Insert Error: Column name or number of supplied values does not match table definition过程 tr_EMPLOYEE2_FORINSERT,第 10 行插入错误:列名或提供的值的数量与表定义不匹配
【发布时间】:2013-11-09 15:27: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
【问题讨论】:
标签:
sql-server-2005-express
【解决方案1】:
一个。我们不知道您的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)))
b.然而,扳机它居然还是坏了。为什么?因为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
然后您就不需要在触发器中编写的其余代码