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

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
1
  • My pleasure ! .
    – Sabin B
    Commented Aug 26, 2015 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.