Solved

Conditionally updating a table value in a table trigger

Posted on 2008-06-24
1
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

752 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