Solved

Conditionally updating a table value in a table trigger

Posted on 2008-06-24
1
190 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 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