我试图在将项目插入表格后创建一个触发器函数但是我收到语法错误并且不确定发生了什么。有人可以帮忙吗?这是我的语法:
GO
CREATE TRIGGER trgAfterInsert ON [DR].[dbo].[Derived_Values]
FOR INSERT
AS
declare @BusinessUnit varchar(75);
declare @Questions varchar(max);
declare @Answer nchar(10);
select @BusinessUnit=i.@BusinessUnit from inserted i;
select @Questions=i.@Questions from inserted i;
select @Answer=i.@Answer from inserted i;
insert into [Main].[dbo].[Derived_Values_Test]
(BusinessUnit,Questions, Answer)
values(@BusinessUnit,@Questions, @Answer);
PRINT 'AFTER INSERT trigger fired.'
GO
答案 0 :(得分:8)
试试这个:
CREATE TRIGGER trgAfterInsert ON [DR].[dbo].[Derived_Values]
FOR INSERT
AS
insert into [Main].[dbo].[Derived_Values_Test]
(BusinessUnit,Questions, Answer)
SELECT BusinessUnit,Questions, Answer
FROM inserted;
PRINT 'AFTER INSERT trigger fired.'
永远不要写像你这样的触发器,它实际上假设只有一行更新。触发器应该使用基于集合的逻辑。
答案 1 :(得分:0)
@Answer
被声明为字符串,而不是表。所以这不起作用:
select @Answer=i.@Questions from @Answer i;
这应该是inserted
吗?
select @Answer=i.@Questions from inserted i;
此外,您似乎正在使用列名称的变量:
select @BusinessUnit=i.BusinessUnit from inserted i;
select @Questions=i.Questions from inserted i;
select @Answer=i.Answerfrom inserted i;