Re: Trigger Problems with UPDATE()
From: Tom Moreau (tom_at_dont.spam.me.cips.ca)
Date: 05/12/04
- Next message: Rohan Hattangdi: "Help me convince the dev manager. Please."
- Previous message: Daniel P.: "Re: procedure that copy over objects from server1 to server2"
- In reply to: anonymous_at_discussions.microsoft.com: "Re: Trigger Problems with UPDATE()"
- Next in thread: Eric D.: "Re: Trigger Problems with UPDATE()"
- Reply: Eric D.: "Re: Trigger Problems with UPDATE()"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 12 May 2004 14:47:43 -0400
That's different. The IF UPDATE() construct only tests if the given column
was mentioned in the SET clause of the UPDATE statement that fired the
trigger. It does not check if the value was indeed changed. Your trigger
code should look like:
CREATE TRIGGER [MasterBF_UPDATE] ON dbo.MasterBF
FOR UPDATE
AS
IF @@ROWCOUNT = 0
RETURN
IF UPDATE(BFOwnerID)
INSERT INTO ActivityLog(ActivityType,
PrimaryKeyValue, TableName)
SELECT CASE WHEN d.BFOwnerID = i.BFOwnerID
THEN 'UPDATE'
ELSE 'TRANSFER'
END, BFID, 'MasterBF'
FROM inserted i
JOIN deleted d on d.PrimaryKeyValue = i.PrimaryKeyValue
GO
-- Tom --------------------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server MVP Columnist, SQL Server Professional Toronto, ON Canada www.pinnaclepublishing.com/sql <anonymous@discussions.microsoft.com> wrote in message news:c09e01c4384f$0c806750$a401280a@phx.gbl... I don't understand your responce. Please explain in laymen terms. I expected the TRIGGER to do exactly as I wrote "If the BFOwnerID field was updated, insert 'TRANSFER', else insert 'UPDATE'." But it's not doing that. TIA, Eric >-----Original Message----- >This trigger will insert a 'TRANSFER' if the UPDATE statement had BFOwnerID >mentioned in the SET clause - regardless of the value to which it is being >set. If you leave it out of the SET clause, then an 'UPDATE' will be >inserted. > >What behavior did you expect? > >-- >Tom > >---------------------------------------------------------- ----- >Thomas A. Moreau, BSc, PhD, MCSE, MCDBA >SQL Server MVP >Columnist, SQL Server Professional >Toronto, ON Canada >www.pinnaclepublishing.com/sql > > >"Eric D." <anonymous@discussions.microsoft.com> wrote in message >news:c2a901c4384c$017c74a0$a001280a@phx.gbl... >Hi, > >I'm having a problem with this code and I don't understand >why. > >Code: >======================= >CREATE TRIGGER [MasterBF_UPDATE] ON dbo.MasterBF >FOR UPDATE >AS >IF UPDATE(BFOwnerID) >BEGIN >INSERT INTO ActivityLog(ActivityType, >PrimaryKeyValue, TableName) >SELECT 'TRANSFER', BFID, 'MasterBF' >FROM INSERTED >END >ELSE >BEGIN >INSERT INTO ActivityLog(ActivityType, >PrimaryKeyValue, TableName) >SELECT 'UPDATE', BFID, 'MasterBF' >FROM INSERTED >END >======================= > >The problem arises in the ELSE statement. It never runs. >Is there a reason? > >TIA, >Eric > > > > > >. >
- Next message: Rohan Hattangdi: "Help me convince the dev manager. Please."
- Previous message: Daniel P.: "Re: procedure that copy over objects from server1 to server2"
- In reply to: anonymous_at_discussions.microsoft.com: "Re: Trigger Problems with UPDATE()"
- Next in thread: Eric D.: "Re: Trigger Problems with UPDATE()"
- Reply: Eric D.: "Re: Trigger Problems with UPDATE()"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|