Reputation: 177
I have to implement a trigger on a table such that some columns get automatically updated each time a row is updated. I am using the following trigger syntax:
create trigger degreedSkillPlan_updated on degreedSkillPlan
after update
as
if (select trigger_nestlevel() )> 1 return;
declare @id varchar;
update degreedSkillPlan set updatedDate = getdate(), lastModifiedDate = getDate() FROM inserted
WHERE inserted.skillPlanId = skillPlanId; ;
but this is not working. I haven't really implemented any trigger before. I have read some tutorials online but I am unable to figure out what is wrong with this. Also, will this work for multiple rows as stated in the accepted answer of >Handling multiple rows in SQL Server trigger
Upvotes: 0
Views: 68
Reputation: 1269873
Your code is wrong on multiple levels:
varchar
with no length is bad and usually does the wrong thing.insert
has only one row is incorrect.begin
/end
block.So:
create trigger degreedSkillPlan_updated on degreedSkillPlan
after update as
begin
if (trigger_nestlevel() ) > 1 return;
update degreedSkillPlan
set updatedDate = getdate(),
lastModifiedDate = getDate()
where skillPlanId in (select i.skillPlanId from inserted i);
end;
Maybe this will fix your problem.
Upvotes: 1