Solved

Creating an SQL trigger in SQL 2005 server

Posted on 2008-06-18
2
158 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

706 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

15 Experts available now in Live!

Get 1:1 Help Now