1

I am trying to create a trigger function after items are inserted into a table but i am getting a syntax error and not sure what is going on. Can someone please help? Here is my syntax:

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
5
  • What is the error message? Commented May 30, 2013 at 20:50
  • here is error: Msg 102, Level 15, State 1, Procedure trgAfterInsert, Line 9 Incorrect syntax near '@BusinessUnit'. Commented May 30, 2013 at 20:50
  • select @BusinessUnit=i.@BusinessUnit from inserted i; Is your column really called @BusinessUnit? Commented May 30, 2013 at 20:51
  • yes it is called BusinessUnit Commented May 30, 2013 at 20:55
  • @moe Then it should probably be select @BusinessUnit=i.BusinessUnit from inserted i;, however @sqlvogel's answer below has a better way of doing the entire thing. Commented May 30, 2013 at 20:56

2 Answers 2

8

Try this:

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

Never write triggers like yours which effectively assumes there will only be one row updated. Triggers should use set based logic.

Sign up to request clarification or add additional context in comments.

4 Comments

thnx, i have tried your method and i did not get any error when i run it but it did not create the trigger in that database. Do i need to declare variables first or this is all what i need. not sure why it did not create the trigger
You don't need any variables at all. Trigger gets created in the database where you run the CREATE TRIGGER statement.
thanks. I am inserting the data a table that is remote server so when i run it i get this error ""The partner transaction manager has disabled its support for remote/network transactions."." Do you know what i need to do in order to resolve this? thanks
If the target is a linked server then I suggest you don't use triggers because that will require a distributed transaction. Use a proc outside a transaction. Or use some other process to replicate the data (Service Broker, SSIS, or even just a remote view on the target server).
0

@Answer is declared as a string, not a table. So this will not work:

select @Answer=i.@Questions from @Answer i; 

Should this be inserted?

select @Answer=i.@Questions from inserted i; 

Also, you appear to be using variables for column names:

select @BusinessUnit=i.BusinessUnit from inserted i;   
select @Questions=i.Questions from inserted i; 
select @Answer=i.Answerfrom inserted i; 

1 Comment

guys i am sorry i don't know how that happened but i updated now all my select statement but still getting the same error. see my initial post. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.