Solved

SQL update trigger based on value

Posted on 2010-11-09
9
273 Views
Last Modified: 2012-05-10
I need a trigger to do the following:

when deletionstatecode is set to 2 in table 'aaa', update statecode value to 1 in table 'bbb' where aaa.activityid = bbb.phonecallid
AAA

aaaid   deletionstatecode    activityid



--------------------------------------

BBB

bbbid   phonecallid   statecode

Open in new window

0
Comment
Question by:sailing_12
9 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 400 total points
ID: 34097736
CREATE TRIGGER aaa_trigger_1
ON aaa
AFTER UPDATE
AS
IF @@ROWCOUNT = 0
    RETURN
IF NOT UPDATE(deletionstatecode)
    RETURN
UPDATE bbb
SET bbb.statecode = 1
FROM bbb
INNER JOIN inserted i ON i.activityid = b.phonecallid
INNER JOIN deleted d ON i.activityid = d.activityid
WHERE i.deletionstatecode = 2
  AND (i.deletionstatecode <> d.deletionstatecode OR d.deletionstatecode IS NULL)
0
 
LVL 22

Expert Comment

by:dportas
ID: 34098053
Do it in a stored procedure, not a trigger. Even better, consider changing the design so that there isn't a dependency between columns in two base tables. Maybe you could derive the same information in a view or query instead.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 34098135
A trigger is safer than a stored proc.  There is no guarantee that a stored proc will be used for all UPDATEs (and/or INSERTs).
0
 
LVL 14

Expert Comment

by:Emes
ID: 34098412
CREATE TRIGGER TRIG2 ON AAA
For update
as

update BBB
set statecode = 1
 
 FROM inserted i
INNER JOIN bbb ON
BBB.phonecallid = i.activityid
and i.deletionstatecode = 2
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sailing_12
ID: 34102700
Scott, on your first inner join, I see alias b on phonecallid - but where is this defined?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 34106392
Sorry, should be bbb.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 34106394
I.e.:

INNER JOIN inserted i ON i.activityid = bbb.phonecallid
0
 

Author Comment

by:sailing_12
ID: 34106564
OK thanks.
0
 

Author Closing Comment

by:sailing_12
ID: 34106569
Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

743 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

10 Experts available now in Live!

Get 1:1 Help Now