Solved

SQL 2008 Update Trigger

Posted on 2010-08-15
6
599 Views
Last Modified: 2012-05-10

I need to make a trigger that updates only rows that have changed

Table 1 Columns
ID, GroupGameID

Table 2 Columns
ID, GroupGameID


The 2 tables are matched by ID.. I need to trigger to update GroupGameID value in table 2 with changed value in table 1
0
Comment
Question by:Leo Torres
[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
6 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33440883
ltorres321,

You can use the DELETED and INSERTED tables to compare the values to tell when there has actually been a change to the GroupGameID field. You can also in newer versions of SQL use IF UPDATE(GroupGameID).
CREATE TRIGGER trg_Tbl1_UpdateTbl2
ON Tbl1
FOR UPDATE
AS
IF UPDATE(GroupGameID)
BEGIN
   UPDATE tto
   SET tto.GroupGameID = tfr.GroupGameID
   FROM Tbl2 tto
   INNER JOIN inserted tfr
      ON tfr.ID = tto.ID;
END
GO

Open in new window

0
 
LVL 8

Author Comment

by:Leo Torres
ID: 33441175
Got this error

Msg 207, Level 16, State 1, Procedure tr_CreateGameDetail, Line 57
Invalid column name 'GameID'.

Not sure how insert is supposed to work here if this is supposed to be an update

 IF (UPDATE (GroupGameID))
   BEGIN
      UPDATE 
         dbo.GI_PlayerGameDetail 
	SET	pd.GroupGameID = i.GroupGameID
		FROM dbo.GI_PlayerGameDetail pd
		INNER JOIN inserted i
			ON i.GameID = pd.GameID

		End		

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 33441317
Is there an actual GameID column ? You said ID before.
IF (UPDATE (GroupGameID))
BEGIN
   UPDATE pd
   SET pd.GroupGameID = i.GroupGameID
   FROM dbo.GI_PlayerGameDetail pd
   INNER JOIN inserted i
      ON i.ID = pd.ID -- use proper ID columns here
END

Open in new window

0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 8

Author Comment

by:Leo Torres
ID: 33441358
Actually the actual Column is GameID i just used ID for the question
I am getting the error on line 8
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 33441581
Got thanks, FYI it was a the link one of the fields was NULL and no match was being made.. Thanks
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33441604
Glad you got it and I could help.

Best regards and happy coding,
Kevin
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 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