我一直在执行此触发器,在插入同一个表时进行合并,但显然我一直收到此错误:
Column name or number of supplied values does not match table definition.
我的触发器:
create trigger [dbo].[Updated_table1]
on [dbo].[table1]
instead of INSERT
AS
BEGIN
set nocount on;
IF(SELECT COUNT(*) FROM INSERTED) = 0
RETURN;
merge into [dbo].[table1] with (HOLDLOCK) as d
using INSERTED as i
on (i.[primarykey] = d.[primarykey])
when not matched then insert
values ('...','...');
END
有什么想法吗?
答案 0 :(得分:0)
在insert语句中明确提及Target
和Source
列名....
像这样........
create trigger [dbo].[Updated_table1]
on [dbo].[table1]
instead of INSERT
AS
BEGIN
set nocount on;
IF(SELECT COUNT(*) FROM INSERTED) = 0
RETURN;
merge into [dbo].[table1] with (HOLDLOCK) as d
using INSERTED as i
on (i.[primarykey] = d.[primarykey])
when not matched then
insert (d.Column1,d.Column2,d.Column3,d.Column4,...)
values (i.Column1,i.Column2,i.Column3,i.Column4,...)
END
答案 1 :(得分:0)
ALTER TRIGGER tr_Emp_forInsert
On Emp
For Insert
AS
BEGIN
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
列名或提供的值与表定义不匹配。