Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 - How to update a field with DELETE trigger

I want to update a field in the same table that the record is being deleted from but not too sure how. The table is pretty simple and has a self reference on it:

UserID    FName   EmailAddress  EmailUserID
1         Frank   [email protected]   2
2         Jane    [email protected]    NULL
3         John    [email protected]    1
4         Brett   [email protected]   2

Here's my trigger:

CREATE TRIGGER [dbo].[CAT_DeleteUser]
   ON  Users
   AFTER DELETE
AS 
BEGIN

    SET NOCOUNT ON;

    UPDATE Users
    SET EmailUserID = NULL
    WHERE EmailUserID = ID_OF_DELETED_USER <-- don't know how to get this

END

So if I delete Jane (UserID = 2), I'd like the trigger to update any record with the EmailUserID of 2. How can I accomplish this?

like image 683
Robert Avatar asked Feb 18 '12 15:02

Robert


People also ask

Does UPDATE trigger fire on DELETE?

A DELETE does not fire UPDATE triggers. If you have a trigger defined to be fired on DELETE and also on UPDATE then it will be executed on a DELETE but that's because it's also a DELETE trigger.

Can we use DELETE in trigger?

To delete a DML trigger In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand the database that you want, expand Tables, and then expand the table that contains the trigger that you want to delete. Expand Triggers, right-click the trigger to delete, and then click Delete.

How can create trigger on insert UPDATE and DELETE in SQL Server?

To test the trigger we can use the code below. INSERT INTO dbo. NestingTest (Test) VALUES (0); UPDATE dbo. NestingTest SET Test = 1 WHERE NestingTestID = 1; DELETE FROM dbo.

What is insert UPDATE DELETE trigger in SQL?

Triggers are database operations which are automatically performed when an action such as Insert, Update or Delete is performed on a Table or a View in database. Triggers are associated with the Table or View directly i.e. each table has its own Triggers. There are two types of Triggers. After and Instead of Triggers.


2 Answers

You may use AFTER DELETE Trigger:

CREATE TRIGGER [dbo].[CAT_DeleteUser] ON  Users
   AFTER DELETE
AS 
BEGIN

    SET NOCOUNT ON;

    UPDATE Users
    SET EmailUserID = NULL
    FROM Users
    JOIN DELETED
      ON Users.EmailUserID = Deleted.UserID

END
like image 119
Oleg Dok Avatar answered Oct 13 '22 14:10

Oleg Dok


Note that if you set up the table with a proper FOREIGN KEY constraint, an after trigger is not going to do the job. The DELETE happens logically before the operations in the trigger, so you will get:

Msg 547, Level 16, State 0, Line 2
The DELETE statement conflicted with the SAME TABLE REFERENCE constraint ...

So if you do want data integrity so that nobody can stuff garbage into the EMailUserID column, you can use an INSTEAD OF trigger as follows:

CREATE TABLE dbo.Users
(
    UserID INT NOT NULL PRIMARY KEY,
    FName  NVARCHAR(32),
    EmailAddress VARCHAR(320),
    EmailUserID INT NULL FOREIGN KEY REFERENCES dbo.Users(UserID)
);
GO

CREATE TRIGGER [dbo].[CAT_DeleteUser] 
ON dbo.Users
INSTEAD OF DELETE
AS 
BEGIN
    SET NOCOUNT ON;

    -- clean up references first
    UPDATE u SET u.EmailUserID = NULL
        FROM dbo.Users AS u
        INNER JOIN deleted AS d
        ON u.EmailUserID = d.UserID;

    -- now delete the row
    DELETE u
    FROM dbo.Users AS u
    INNER JOIN deleted AS d
    ON u.UserID = d.UserID;
END
GO

Sample try create a copy of dbo.Users, as above, in tempdb, then run this):

INSERT dbo.Users(UserID, FName, EmailAddress, EmailUserID) VALUES
(1,'Frank','[email protected]',2   ),
(2,'Jane ','[email protected] ',NULL),
(3,'John ','[email protected] ',1  ),
(4,'Brett','[email protected]',2  );

SELECT * FROM dbo.Users;
GO

DELETE dbo.Users WHERE UserID = 2;
GO

SELECT * FROM dbo.Users;
GO

DROP TABLE dbo.Users;
GO
like image 35
Aaron Bertrand Avatar answered Oct 13 '22 13:10

Aaron Bertrand