Solved

Conditionally updating a table value in a table trigger

Posted on 2008-06-24
1
200 Views
Last Modified: 2010-03-19
I have defined a trigger on a table which fires everytime a row is inserted.  When a row is inserted, I update a second table's 'status' column with the new row's status field as follows:

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

This is working fine.  I however have an additional requirement:  If the i.status value is 'Offline' then I would also like to update the Customers table 'Active' field to 'NO'.

How can I do this in my SQL code?

Thanks

Chris
0
Comment
Question by:CLoucas
1 Comment
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21857793
CREATE TRIGGER Status_insert_trigger
ON Status
AFTER INSERT
AS
UPDATE Customers
SET Status = i.status,
Active = CASE WHEN i.Status = 'Offline' THEN 'NO' ELSE Active END
FROM Customers
INNER JOIN Inserted i ON i.ID = Customers.ID
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

828 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