Solved

Creating an SQL trigger in SQL 2005 server

Posted on 2008-06-18
2
159 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:
ScottPletcher 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 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

14 Experts available now in Live!

Get 1:1 Help Now