1
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).

2
  • Is there any relationship between table a and table d? Mar 3, 2011 at 19:24
  • Forgot to mention yes these tables have a relationship of primary and foreign key
    – dca
    Mar 3, 2011 at 19:35

2 Answers 2

1

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
15
  • i am very new to sql. if you can explain me please. what is (de) and (i)
    – dca
    Mar 3, 2011 at 19:41
  • de and i are aliases for the Deleted and Inserted tables. It's just a short-hand way of referring to the table names in the query. Mar 3, 2011 at 19:42
  • Thanks, i am not deleting any data from any table. its like if user change any information in one table then second table one of the field will be 1. so behalf of 1 or 0 later on can be deiced if data need to update in master database or not. sry my English is not that great.
    – dca
    Mar 3, 2011 at 19:49
  • @user643092: I understood that you're not deleting data. The Deleted table is used here because it contains the "before" version of the rows that were impacted by your update statement. Mar 3, 2011 at 19:52
  • Thanks so much. its work. one more question. if i have more than one field , like first name and last name.
    – dca
    Mar 3, 2011 at 20:08
0

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
4
  • actually i have 15 fields in Table (a) but if Three of them update, then table (d) upload field get the value of 1 other wise its 0 by default
    – dca
    Mar 3, 2011 at 19:33
  • i Forgot to mention yes these tables have a relationship of primary and foreign key
    – dca
    Mar 3, 2011 at 19:34
  • just add IF UPDATE(name) AND UPDATE(field2) and UPDATE(field3) if you need to do something when that condition does not work just add an ELSE statement
    – Necronet
    Mar 3, 2011 at 20:20
  • this code is working , but its change table d (field upload) when any field data change in table a.
    – dca
    Mar 3, 2011 at 21:21

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.