开发者

Using trigger in one table and updating other table

CREATE TRIGGER dbo.YourTrigger
ON a
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;

IF NOT UPDATE(name)
RETURN

UPDATE d
set upload = 1
FROM d
END

This is the开发者_高级运维 code, but it's not working the way i want it. in this case its updating my upload field from Table (d) when every any record change in table (a) . i want upload field in table (d) to be change only when (name field) change in Table (a).


You use the special Inserted and Deleted tables inside a trigger to identify which rows have been affected. For an update trigger, Deleted contains the "before" version of the rows and Inserted contains the "after" version of the rows.

CREATE TRIGGER dbo.YourTrigger
ON a
AFTER UPDATE
AS
BEGIN
    SET NOCOUNT ON;

    IF UPDATE(name)
        UPDATE d
            set upload = 1
            FROM Inserted i
                INNER JOIN Deleted de
                    ON i.EmpId= de.EmpId
                INNER JOIN d
                    ON i.EmpId= d.RecId
            WHERE i.name <> de.name
END


Why dont you do it a little cleaner and simple since you are only going to update if name is updated...

CREATE TRIGGER dbo.YourTrigger
ON a
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;

IF UPDATE(name)
BEGIN
UPDATE d
set upload = 1
FROM d
END
ELSE
BEGIN
       --HERE GOES CODE
END
END
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜