Solved

Update Trigger - multiple fields

Posted on 2004-08-19
10
259 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

813 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now