Link to home
Start Free TrialLog in
Avatar of rsanglim
rsanglim

asked on

Update Trigger - multiple fields

Greetings Experts...I'm trying to update 2 fields in Table2 when the same fields for the same ID have been updated in Table1. This may be simple, but I have a deadline that is FAST approaching. Can somebody please help me resolve the following statement/error:
----------------------------------------------------------------------------------
CREATE TRIGGER Update_FieldB_FieldC ON Table1 FOR UPDATE AS

IF UPDATE (FieldB) or UPDATE (FieldC)
BEGIN
UPDATE Table2
SET Table2.FieldB = inserted.FieldB ,
Table2.FieldC = inserted.FieldC
FROM inserted.deleted
WHERE Table2.FieldA = inserted.FieldA     'FieldA is ID in Table1, inserted to Table2 via trigger (not ID in Table2)
go
-------------------------------------------------------------------------------------
Server: Msg 170, Level 15, State 1, Procedure Update_Rig_Loc, Line 9
Line 9: Incorrect syntax near 'FieldA'.
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

CREATE TRIGGER Update_FieldB_FieldC ON Table1 FOR UPDATE AS

IF UPDATE (FieldB) or UPDATE (FieldC)
BEGIN
UPDATE Table2
SET Table2.FieldB = inserted.FieldB ,
Table2.FieldC = inserted.FieldC
FROM inserted, table2
WHERE Table2.FieldA = inserted.FieldA     'FieldA is ID in Table1, inserted to Table2 via trigger (not ID in Table2)
go
Avatar of BillAn1
BillAn1

Nearly there ...... try this

CREATE TRIGGER Update_FieldB_FieldC ON Table1 FOR UPDATE AS

IF UPDATE (FieldB) or UPDATE (FieldC)
BEGIN
UPDATE Table2
SET Table2.FieldB = inserted.FieldB ,
Table2.FieldC = inserted.FieldC
FROM inserted.deleted
WHERE Table2.FieldA = inserted.FieldA  
END  
Comments are preceded by -- not ', and you need an 'end' for the 'begin', so try this:


CREATE TRIGGER Update_FieldB_FieldC ON Table1 FOR UPDATE AS

IF UPDATE (FieldB) or UPDATE (FieldC)
BEGIN
UPDATE Table2
SET Table2.FieldB = inserted.FieldB ,
Table2.FieldC = inserted.FieldC
FROM inserted
WHERE Table2.FieldA = inserted.FieldA     --FieldA is ID in Table1, inserted to Table2 via trigger (not ID in Table2)
END
go
Or:
CREATE TRIGGER Update_FieldB_FieldC ON Table1 FOR UPDATE AS

IF UPDATE (FieldB) or UPDATE (FieldC)
BEGIN
UPDATE Table2
SET Table2.FieldB = inserted.FieldB ,
Table2.FieldC = inserted.FieldC
FROM inserted
          Inner Join Table2 On inserted.FieldA = Table2.FieldA  'FieldA is ID in Table1, inserted to Table2 via trigger (not ID in Table2)
go
ASKER CERTIFIED SOLUTION
Avatar of BillAn1
BillAn1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Scott as usual :) has it right.
Or, perhaps very slightly clearer:


CREATE TRIGGER Update_FieldB_FieldC
ON Table1
FOR UPDATE AS
IF UPDATE (FieldB) or UPDATE (FieldC)
BEGIN
UPDATE Table2
SET Table2.FieldB = inserted.FieldB ,
Table2.FieldC = inserted.FieldC
FROM Table2
INNER JOIN inserted ON Table2.FieldA = inserted.FieldA  --FieldA is ID in Table1, inserted to Table2 via trigger (not ID in Table2)
END
go
Avatar of rsanglim

ASKER

The comments aren't part of the statement. I included to try to be more descriptive. Sorry for any confusion, and I'll properly notate in the future (my Access background, you know). Working the solutions now...
pts to BillAn1 - works great!

The ScottPletcher / acperkins solutions produced the following:

Server: Msg 207, Level 16, State 3, Procedure Update_Rig_Loc, Line 4
Invalid column name Table2.

acperkins first solution produced original error.

Thanks, everyone, for the help...REALLY appreciate it.
I suspect you added a carriage return/line feed on this line:

INNER JOIN inserted ON Table2.FieldA = inserted.FieldA  --FieldA is ID in Table1, inserted to {CR?} Table2 via trigger (not ID in Table2)


That was a *single* line, not two.  It might be clearer to see like this:

--FieldA is ID in Table1, inserted to Table2 via trigger (not ID in Table2)
INNER JOIN inserted ON Table2.FieldA = inserted.FieldA