Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2009, 11:01 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default SQL AfterUpdate Trigger

i am trying to develop an afterupdate trigger of my table PROJECTREQUESTEDCHANge_Test that will runs different bits of code depending on the bit fields that are changed, below are the test tables i have set up and also the trigger that i have written. the main problems are

1. getting a field count from the other tables
i've used IF (SELECT FIELDTHREE FROM INSERTED) <> 0, but i ma not sure if this is the best way to do it

2. I seem to have too many begin and ends

I would be grateful if someone could have a look at my trigger and maybe point me in the right direction.

many thanks


TableName : PROJECTREQUESTEDCHANge_Test
PK FIELD ProjectID, bigint
fieldOne bit
fieldTwo bit
fieldThree bit
fieldfour bit
fieldfive bit
TableName : TABLE_B
PK FIELD ProjectID, bigint

TableName : TABLE_C
PK FIELD ProjectID, bigint

Create Trigger U_GenerateRequestedChange
on PROJECTREQUESTEDCHANge_Test
for UPDATE
AS
DECLARE @PROJECTID BIGINT
@PROJECTID = SELECT PROJECTid from inserted

--check this field
IF UPDATE(FIELDONE)
BEGIN
IF (SELECT FIELDONE FROM INSERTED) <> 0 --fieldone is a bit datatype, so if its not equal to 0 then it must 1 '
BEGIN
--if fieldone = 1 then, then check table_b to see if there are any records in table_b with a projectID that is the same as a field in inserted called requestedchangeID
IF (Select Count(*) FROM TABLE_B WHERE PROJECTID = @PROJECTID) <= 0
--if there isn't any records int table_B, then run the code
BEGIN
--RUN CODE
END
END
--check this field
IF UPDATE(FIELDTWO)
BEGIN
IF (SELECT FIELDTWO FROM INSERTED) <> 0 --fieldtwo is a bit datatype, so if its not equal to 0 then it must 1 '
BEGIN
--if fieldone = 1 then, then check table_b to see if there are any records in table_b with a projectID that is the same as a field in inserted called requestedchangeID
IF (Select Count(*) FROM TABLE_C WHERE PROJECTID = @PROJECTID) <= 0
--if there isn't any records in TABLE_C, then run the code
BEGIN
--RUN CODE
END
END

--check this field
IF UPDATED(FIELDTHREE)
BEGIN
IF (SELECT FIELDTHREE FROM INSERTED) <> 0 --FIELDTHREE is a bit datatype, so if its not equal to 0 then it must 1 '
BEGIN
IF (SELECT FIELDFOUR FROM INSERTED) <> 0 -- fieldfour is also a bit datatype
--if fieldthree and fieldfour are both 1
BEGIN
--RUN THE CODE TO UPDATE THE CORRESPONDING REQUESTED CHAGNE RECORD
END
IF (SELECT FIELDFIVE FROM INSERTED) <> 0 --fieldfive is also a bitdatatype
--if fieldthree and fieldfive
BEGIN
--RUN THE CODE TO UPDATE THE CORRESPONDING REQUESTED CHAGNE RECORD
END
END
END

/
 
Old May 20th, 2009, 12:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by debbiecoates View Post
1. getting a field count from the other tables
i've used IF (SELECT FIELDTHREE FROM INSERTED) <> 0, but i ma not sure if this is the best way to do it
I don't think I understood that. Looking at the SQL code, I wonder what does FIELD count mean there?

Quote:
Originally Posted by debbiecoates View Post
2. I seem to have too many begin and ends
You should be looking at indentation (tabbing the lines accordingly) of code to avoid counting how many BEGINs and ENDs are actually there and how many match.



Code:
BEGIN
...
END
That should pretty much solve the main problems

Hope that helps.

Cheers,
__________________
- Vijay G

Last edited by happygv; May 20th, 2009 at 12:44 PM.. Reason: To add code sample





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL+Trigger arup_kc SQL Server 2000 5 March 20th, 2007 08:06 AM
Trigger Problem in sql server monika.vasvani SQL Language 1 March 1st, 2007 07:09 AM
Trigger in SQL Server 2000 deniscuba SQL Server 2000 4 April 1st, 2005 11:37 AM
Emulate Access AfterUpdate Event Kenny Alligood VB Databases Basics 1 July 7th, 2004 11:16 PM
SQl Server 2000 Trigger everest SQL Server 2000 5 April 26th, 2004 01:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.