Solved

Update Trigger - multiple fields

Posted on 2004-08-19
10
257 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
Comment Utility
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
Comment Utility
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:ScottPletcher
Comment Utility
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
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Scott as usual :) has it right.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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
Comment Utility
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
Comment Utility
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:ScottPletcher
Comment Utility
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 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

11 Experts available now in Live!

Get 1:1 Help Now