• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

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
0
DStarkey
Asked:
DStarkey
  • 2
  • 2
1 Solution
 
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
 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now