Solved

Update Trigger - multiple fields

Posted on 2004-08-19
10
265 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

690 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