I'm writing a trigger that when a new record is added to one database (basically a new customer being added), it then adds some of those details to a second database which is used by another internal system.
I've come up with the following code :
create trigger NewEntity on [Intranet-Test].[dbo].[vpw_Entities] after insert as declare @type_id int declare @entity_id int declare @Is_Active bit declare @Entity_Name nvarchar(256) @Is_Active=inserted.IsActive @entity_id=inserted.EntityID @Entity_Name=inserted.EntityName @type_id=inserted.TypeID if @type_id in ('2','3') begin insert into [TimeTracking-Test].[dbo].[clients] (clientid, active, clientname) values (@entity_id, CAST(@Is_Active as INT), @Entity_Name) end go
but within SSMS it's indicating a syntax error near the "@Is_Active=inserted.IsActive" line, which appears to be related to the declare statements (eg if I add anything between them the new line gets the syntax error).
Looking online I can't find any indication of why I'd be getting this error, though I'm assuming that variable declaration is actually allowed within a trigger.
Anyone know why this isn't working? If it is the declare statements at fault, do you have any ideas how to achieve the same thing without them? I tried putting the inserted.entityname etc references directly into the values part of the insert statement but that also gave me an error.