1

我正在尝试在将项目插入表后创建一个触发器函数,但我收到语法错误并且不确定发生了什么。有人可以帮忙吗?这是我的语法:

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
4

2 回答 2

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.'

永远不要编写像你这样有效地假设只会更新一行的触发器。触发器应使用基于集合的逻辑。

于 2013-05-30T20:53:55.413 回答
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; 
于 2013-05-30T20:51:23.393 回答