Stored procedure and trigger

Posted by noober on Stack Overflow See other posts from Stack Overflow or by noober
Published on 2011-01-11T00:49:26Z Indexed on 2011/01/11 0:53 UTC
Read the original article Hit count: 194

Hello all,

I had a task -- to create update trigger, that works on real table data change (not just update with the same values). For that purpose I had created copy table then began to compare updated rows with the old copied ones. When trigger completes, it's neccessary to actualize the copy:

UPDATE CopyTable SET
    id = s.id,
    -- many, many fields
FROM MainTable s WHERE s.id IN (SELECT [id] FROM INSERTED)
                 AND CopyTable.id = s.id;

I don't like to have this ugly code in trigger anymore, so I has extracted it to a stored procedure:

CREATE PROCEDURE UpdateCopy AS
BEGIN
UPDATE CopyTable SET
    id = s.id,
    -- many, many fields
    FROM MainTable s WHERE s.id IN (SELECT [id] FROM INSERTED)
    AND CopyTable.id = s.id;
END

The result is -- Invalid object name 'INSERTED'. How can I workaround this?

Regards,

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2008