Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 February 14th, 2008, 09:39 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 After Update Trigger

can anyone help me

i've created a trigger on one of my tables (called PROJECT), that I beleve should fire if I either add a new record to the table, or update an existing record.

The trigger is working fine if I update the record, but its not firing when i create a new record, and I dont understand why,

This is my trigger

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'TR_TrackUsers' AND type = 'TR')
   DROP TRIGGER TR_TrackUsers
GO


CREATE TRIGGER TR_TrackUsers
ON project
FOR INSERT, UPDATE
AS

DECLARE @PROJECTID BIGINT,
@USERID INT,
@NewUserID int

SELECT @PROJECTID = i.ProjectId,
   @USERID = i.UserID
FROM inserted i

SELECT @NewUserID = d.UserID
FROM deleted d


if @USERID <> @NewUserID

    BEGIN
    INSERT INTO
    ALLOCATIONLOG
    (ProjectID, UserID )
    VALUES
    (@PROJECTID, @USERID)

 
Old February 14th, 2008, 02:01 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you add a new record then @NewUserID will be NULL as DELETED is empty. Comparing NULL with an ID will give NULL not FALSE. Try adding OR @NewUserID IS NULL to the test.
You could also LEFT JOIN the INSERTED table to the DELETED on UserID and select the row WHERE DELETED.UserID IS NULL to get only the new record. (This trigger will work badly if you ever do a multiple row update.)

Why do you bother with having the trigger fire for updates aa then INSERTED.UserID will equal DELETED.UserID and nothing will happen, or is there more to the trigger that you haven't shown?

--

Joe (Microsoft MVP - XML)
 
Old February 15th, 2008, 04:55 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

Brilliant, Thanks very much Joe

Debbie






Similar Threads
Thread Thread Starter Forum Replies Last Post
Create trigger on update bvpsekhar MySQL 23 May 4th, 2007 01:01 AM
insert,update trigger rolle SQL Server 2000 3 September 29th, 2005 08:41 PM
Problem with update trigger gbrown SQL Language 2 September 4th, 2004 12:33 PM
Help With INSERT + UPDATE Trigger HenryE SQL Server 2000 1 December 11th, 2003 06:26 PM
Insert Update Trigger mstuart60 SQL Server 2000 10 September 30th, 2003 06:54 AM





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