Solved

Creating an SQL trigger in SQL 2005 server

Posted on 2008-06-18
2
160 Views
Last Modified: 2010-03-19
I have a table in SQL server 2005.   I would like to have the following scenario:
Whenever a new row is inserted into this table I would like to update another existing table.  One column of this table is to be updated with the value inserted into one of the original table's columns.

Example:
Table in which rows are inserted:

Status Table:
-----------------
ID
Status

Existing table to update:

Customers Table:
---------------------
ID
Name
LastName
Status

When a new row is added into the Status table (signalling status change), I would like the matching record's Status field in the Customer table to take on the value inserted in the Status table.

I would appreciate if you could post an appropriate T-SQL statement to enter into the trigger definition.

Thanks
0
Comment
Question by:CLoucas
2 Comments
 
LVL 2

Expert Comment

by:AntonyDN
ID: 21812782
If the ID in both tables refers to the customer, then this should do it ....




UPDATE Customer
SET Customer.Status = StatusTable.Status 
FROM Customer
JOIN StatusTable 
ON Customer.ID = StatusTable.ID
WHERE StatusTable.ID = @@IDENTITY

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 21813278
CREATE TRIGGER Status_insert_trigger
ON Status
AFTER INSERT
AS
UPDATE Customers
SET Status = i.status
FROM Customers
INNER JOIN Inserted i ON i.ID = Customers.ID
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

832 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