0

I want this trigger to be fired after an insert is made with the text, this is what I have so far

ALTER TRIGGER [xxx]
 ON [dbo].[Main] 
 AFTER INSERT
AS 
BEGIN
 SET NOCOUNT ON;
 INSERT INTO dtsx.dbo.main(refno,subject,recdate,dateind,officefrom,author,status)
 SELECT refno,subject,datereceived,'REC','TEST',author,StatDesc
 FROM dbo.Main LEFT OUTER JOIN
 dbo.DocStat ON dbo.Main.Status = dbo.DocStat.StatID
END

I just want the last record i created to be inserted but instead all the records in the source table (dbo.main) are inserted to the target table (dtsx.dbo.main). Where do place the INSERTED clause, if any? TIA

Marco
3,7205 gold badges25 silver badges31 bronze badges
asked Aug 26, 2015 at 8:11
0

1 Answer 1

1

Every trigger have some internal tables , INSERTED and DELETED. In this case, we use only the INSERTED table, which hold the records inserted.

ALTER TRIGGER [xxx] ON [dbo].[Main] 
AFTER INSERT 
AS 
BEGIN
 SET NOCOUNT ON;
 INSERT INTO dtsx.dbo.main(refno,subject,recdate,dateind,officefrom,author,status)
 SELECT refno,subject,datereceived,'REC','TEST',author,StatDesc 
 FROM INSERTED AS I 
 LEFT OUTER JOIN dbo.DocStat AS DS
 ON I.Status = DS.StatID
END
answered Aug 26, 2015 at 8:18
1
  • My pleasure ! . Commented Aug 26, 2015 at 11:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.