Sql Server Trigger

I am really new to sql server and the whole trigger concept and I am running into  some issues. What I am trying to do is create a trigger that updates the current_workitem table with the current information after a new record is inserted into the workitemhistory table . Not sure if I am even close. Is there a good example I could look at to help give me some direction?

ALTER TRIGGER [CMSC].[WorkItem_UPD] ON [CMSC].[WorkItemHistory]
After Insert
AS

BEGIN

SET NOCOUNT ON;

DECLARE @Wkid int
DECLARE @Histid int

SELECT Wkid = @Wkid, Histid = @Histid FROM Deleted

Update CMSC.WorkItems
Set Histid = @Histid
where wkid = @wkid
   
END
DStarkeyAsked:
Who is Participating?
 
Aneesh RetnakaranDatabase AdministratorCommented:
ALTER TRIGGER [CMSC].[WorkItem_UPD] ON [CMSC].[WorkItemHistory]
After Insert
AS

BEGIN

SET NOCOUNT ON;

Update w
Set Histid = i.Histid
FROM CMSC.WorkItems w
inner join inserted i on w.wkid = i.wkid
   
END
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this will work better:
ALTER TRIGGER [CMSC].[WorkItem_UPD] ON [CMSC].[WorkItemHistory]
After Insert
AS

BEGIN

SET NOCOUNT ON;
Update wi
  SET histid = i.histid
FROM CMSC.WorkItems wi
JOIN INSERTED i
  ON i.wkid = wi.wkid

END

Open in new window

0
 
DStarkeyAuthor Commented:
Thanks that did it...
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
which is the same as mine, or did I miss something :)
0
 
DStarkeyAuthor Commented:
Sorry I selected the wrong one...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.