Solved

Update Trigger - multiple fields

Posted on 2004-08-19
10
264 Views
Last Modified: 2006-11-17
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'.
0
Comment
Question by:rsanglim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11846577
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
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11846582
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  
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 11846584
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11846585
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
0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 500 total points
ID: 11846592
Oops - didn't spot the 'deleted' word

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
WHERE Table2.FieldA = inserted.FieldA  
END  
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11846597
Scott as usual :) has it right.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 11846599
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
0
 

Author Comment

by:rsanglim
ID: 11846701
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...
0
 

Author Comment

by:rsanglim
ID: 11846831
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.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 11851467
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  
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question