【问题标题】: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
    

    然后您就不需要在触发器中编写的其余代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-22
      • 2016-04-17
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 2022-09-28
      相关资源
      最近更新 更多