SQL How to restrict parent row from updates once child added? - sql

I use foreign keys to prevent rows from being deleted in some SQL tables once a child uses the key. However, now I want to be able to restrict any column of the parent table from updates once the child uses the key.
For example if the parent table is
[ID],[First Name],[Last Name]
And the child table is
[ListID],[Emp ID]
The foreign key prevents me from deleting or updating [ID] in the parent table however, it doesn't stop me from modifying [First Name]. Is there a way to set up foreign keys to prevent this?
Thanks.

you could use instead of trigger.
CREATE TABLE [dbo].[ParentTable]
(
ID INT, FirstName VARCHAR(50), LastName VARCHAR(50)
)
go
CREATE table ChildTable(EmpID INT)
go
CREATE TRIGGER [dbo].[ParentTable_InsteadOfUPDATE]
ON [dbo].[ParentTable]
INSTEAD OF UPDATE
AS
BEGIN
DECLARE #ID INT, #FirstName VARCHAR(50), #LastName VARCHAR(50)
SELECT #ID = INSERTED.ID,
#FirstName = INSERTED.FirstName,
#LastName = INSERTED.LastName
FROM INSERTED
IF EXISTS(select top 1 1 from DBO.ChildTable Where EmpID=#ID )
BEGIN
IF UPDATE([ID]) OR UPDATE([FirstName]) OR UPDATE([LastName])
BEGIN
RAISERROR('These fields cannot be updated.', 16 ,1)
ROLLBACK
END
END
ELSE
BEGIN
Update [ParentTable] SET FirstName=#FirstName,LastName=#LastName Where ID=#ID;
END
END
go

Try this:
CREATE TRIGGER trg_name ON parent_table INSTEAD OF UPDATE
AS
BEGIN
UPDATE parent_table
SET [FirstName] = I.[firstname]
,[LastName] = I.[lastname]
FROM parent_table A
INNER JOIN inserted B
ON A.[ID] = B.[ID]
LEFT JOIN child_table CT
ON A.[ID] = CT.[ListID] -- or [EmpID] - am not sure about the relation
WHERE CT.[LsitID] IS NULL -- where the element from parent_table does not have element in the child table
END
Also, always try to handle the logic in triggers in batches in order not to hurt the performance.

Related

SQL Server 2008: Sql Insert/Update into another table using insertion IDs output from another table

I have a procedure for insert in multiple dependent tables (update in case record exist). I have input parameters as comma separated string which I am reading in table.
After 1st insertion I am getting InsertedIds in another table variable.
I am struggling over how to do insert in 2nd table. I have following input parameters for 2nd table:
Declare #IdsToBeUpdated table (primary key identity pkey, id int) -- values are 1,-1,3,-1
Declare #CommentsTobeInserted table( primary key identity pkey, comment varchar (max)) -- values are 'com1', 'com2', 'com3'
-1 input in table #IdsToBeUpdated depicts insertion for the corresponding rows in all input tables and value other than -1 depicts that records at that pkey value in all other tables (#CommentsTobeInserted table as in example) have to be updated.
So after first insertion I am getting the inserted ids for rows with -1 value. #InsertedIds = 4,5
So my Ids list logically would become.. 1,4,3,5.
But I am stuck now how to insert/update records in second table respectively.
2nd table would be like follows:
Pkey Primary key identity, commentIds(coming from inserted ids and #IdsToBeUpdated), comments.
I have added one more InsertedIds column in #CommentsTobeInserted. If I could fill it with right InsertedId against correct row, I guess I would be able to do insert/update in 2nd table. And where the value is Id in new column I would do insert, where it's null, I would perform update using #IdsToBeUpdated table.
But currently none of my approach is working. I am new to SQL. Any help would be highly appreciated.
Following is some portion of script for better understanding. I have added first insertion script as well.
USE [Demo]
GO
/****** Object: StoredProcedure [dbo].[USP_NewRequest_Insert] Script Date: 2/11/2016 2:50:34 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[USP_NewRequest_Insert]
-----------------------------------
--------- SomeInput parameters
---------for first insertion omitted
--------------------------------
#IdsToBeUpdated varchar(MAX), --Values are 23|-1|32|-1|-1
#CommentList AS VARCHAR(MAX), --Values 'C2'|'C4'|'C5'|'C6'|'C7'
#MCodeList VARCHAR(MAX), --Values 'M2'|'M4'|'M5'|'M6'|'M7'
#CustomerIdsList VARCHAR(MAX), ----Values 'Cust2'|'Cust4'|'Cust5'|'Cust6'|'Cust7'
#ReturnValue AS INT OUTPUT,
------------------Data is in the order it has to be inserted.. where IDs exist in #IdsToBeUpdated, updation would be done. For -1 values, insertion.
AS
BEGIN
BEGIN TRANSACTION
--------------------------Split input strings aand insert in Table variable----------------------------
declare #MCodes Table (pkey int primary key identity, MCode varchar(20))
insert into #MCodes select s.Item from [dbo].UDF_SplitString(#MCodeList, '|') s
declare #CusCodes Table (pkey int primary key identity, CusCode varchar(200))
insert into #CusCodes select s.Item from [dbo].UDF_SplitString(#CustomerIdsList, '|') s
declare #ReqDetailsIds Table (pkey int primary key identity, Id Int)
insert into #ReqDetailsIds select Convert(INT,RTRIM(LTRIM(s.Item))) from [dbo].UDF_SplitString(#IdsToBeUpdated, '|') s
where s.Item is not null and RTRIM(LTRIM(s.Item)) <>''
declare #ProductComments Table (pkey int primary key identity, Comment Varchar(max), insertedId int null)
insert into #ProductComments(Comment) select s.Item from [dbo].UDF_SplitString(#CommentList, '|') s
DECLARE #intErrorCode int;
------------------------------------------------------------------------------------------------------------
-----------------First Insertion which returns inserted IDs for 2nd insertion
------------------------------------------------------------------------------------------------------------------
---Insert/Update product details in [RequestDetails]
Declare #InsertedIDList Table (pkey int primary key identity, ID int); --------Table to read inserted Ids. Used in query below
-----------------------Insert query in case Detail id = -1
INSERT INTO [dbo].[RequestDetails]
[MCode]
,[CustomerIds]
,[ExpectedVolume]------Some parameters coming for first insertion in input
,[StatusCode])
Output INSERTED.ReqDetailId Into #InsertedIDList(ID)
SELECT A.MCode, B.CusCode, E.Vol,1
FROM #MCodes A
JOIN #CusCodes B ON B.pkey = A.pkey
JOIN #ExpectedVols E ON E.pkey = A.pkey
JOIN #ReqDetailsIds G ON G.pkey = A.pkey
WHERE G.Id = -1 --If id = -1, insert
---------------------------Update Query for rest records
UPDATE [dbo].[RequestDetails]
SET [MCode] = upd.MCode
,[CustomerIds] = upd.CusCode
,[ExpectedVolume] = upd.ExVol
,[StatusCode] = 1
FROM(
SELECT A.MCode, B.CusCode, E.ExVol, G.Id
FROM #MCodes A
JOIN #CusCodes B ON B.pkey = A.pkey
JOIN #ExpectedVols E ON E.pkey = A.pkey
JOIN #ReqDetailsIds G ON G.pkey = A.pkey
WHERE G.Id <> -1
) upd
WHERE upd.Id = dbo.RequestDetails.ReqDetailId
IF(##Error<>0)
BEGIN
SET #intErrorCode = ##Error
GOTO ERROR
END
ELSE
BEGIN
SET #ReturnValue=1
END
---------------------------------------------------------------------------
----------------------------Now similarly I have to do insert/update in Comments Table. But
----------------------------Comments table has RequestDetails Id column as foreign key. So
----------------------------now the challange is to add the rows with Inserted ID where value was = -1
----------------------------in input. We have got the IDs corresponding to -1 values from above insertion
----------------------------in the #InsertedIDList Table variable
-----------------------------------------------------------------------------------------
----------------------------Following is what I have tried so far. But I am not able to insert
----------------------------correct InsertedId against correct record.
----------------------------------------------------------------------------------------
-----------------------Here I tried to insert the new generated ids against corresponding comments in table variable.
-----------------------So that I can perform insert where value is not null. As NULL would be inserted where new ID has not been created
-----------------------and corresponding updated ID exists in input (Values not equal to -1)
-------------------------------------------------------------------------------------------------
Update #ProductComments set insertedId = i.ID from ---------This query is not working
(select A.pkey, B.id as detailId, row_number() over (order by (select 0)) as row_num from
#ProductComments A
JOIN #ReqDetailsIds B ON B.pkey = A.pkey) as mappedNewIds right join
#InsertedIDList i on i.pkey = mappedNewIds.row_num
where mappedNewIds.pkey = [#ProductComments].pkey
----Insert in CommentsTable for New Comments against request
---------------------------------
INSERT INTO [dbo].CommentsTable
( ReqDetailId, Comments, CreatedOn )
SELECT A.insertedId, A.Comment, GETDATE()
FROM #ProductComments A
where A.insertedId is not null
-----Update Query
------------------------------------------------------------------------------------------
UPDATE [dbo].[CommentsTable]
SET [ReqDetailId] = upd.Id
,[Comments] = upd.Comment
,[CreatedOn] = GetDate()
FROM(
SELECT A.Comment, B.Id
FROM #ProductComments A
JOIN #ReqDetailsIds B ON B.pkey = A.pkey
WHERE A.insertedId is not null
) upd
WHERE upd.Id = dbo.CommentsTable.ReqDetailId
END
select * from CommentsTable;
---------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
IF(##Error<>0)
BEGIN
SET #intErrorCode = ##Error
GOTO ERROR
END
ELSE
BEGIN
SET #ReturnValue=1
END
COMMIT TRANSACTION
ERROR:
IF (#intErrorCode <> 0) BEGIN
SET #ReturnValue = 0;
ROLLBACK TRANSACTION
END
END

Add extra column in insert with instead of trigger

EDIT 1:Basically I want to do
INSERT INTO Users(col1, col2, col3) when Users only have col1 and col2 , col3 doesn't exist.
I have a table UsersAddresses with two int fields "User" and "Addresses" which that binds my table Users to another table Addresses. I need to audit any change made to my user so I have a trigger on my Users that works fine, any changes made to my Users table creates a line in the audit table.
The problem is that I also need to trigger the same trigger if I add an addresses to my user. So I create a trigger on my UsersAddresses that will do an update of the Users table. But I need to have the modifierId aswell (to know who wanted to make this change) So I figure out I should alter the way I insert in UsersAddresses to add this Id. I've tried the following, adding a fake "ModifierId" but as expected I get "invalid column name" when I try to add this trigger.
Do you know what I should do ?
Thankfully
CREATE TRIGGER UpdateUser
ON [dbo].[UsersAddresses]
INSTEAD OF INSERT
AS
DECLARE #User INT;
DECLARE #Address INT;
DECLARE #ModifierId INT;
SELECT #User = [User] FROM inserted;
SELECT #Address = [Address] FROM inserted;
SELECT #ModifierId = [ModifierId] FROM inserted;
INSERT INTO [UsersAddresses]([User], [Address])
VALUES (#User, #Address);
UPDATE [Users]
SET [CreatorId] = #ModifierId
WHERE [Id] = #User;
Edit Solution: Credits to Michael
use context_info with a procedure.:
CREATE TABLE [dbo].[UsersAddresses] (
[User] INT NOT NULL,
[Address] INT NOT NULL,
CONSTRAINT [PK_UsersAddresses] PRIMARY KEY CLUSTERED ([User] ASC, [Address] ASC),
CONSTRAINT [FK_ToUser] FOREIGN KEY ([User]) REFERENCES [dbo].[Users] ([Id]),
CONSTRAINT [FK_ToAddress] FOREIGN KEY ([Address]) REFERENCES [dbo].[Addresses] ([Id])
);
GO
CREATE TRIGGER [ChangeToUsersAddresses]
ON [dbo].[UsersAddresses]
FOR DELETE, INSERT, UPDATE
AS
BEGIN
IF ( (SELECT COUNT(*) FROM inserted) > 1)
THROW 51000, 'To many records', 1
DECLARE #UserId INT;
IF (SELECT [User] FROM inserted) IS NOT NULL
SELECT #UserId = [User] FROM inserted
ELSE
SELECT #UserId = [User] FROM deleted
INSERT INTO [Audit_Users](UserId,ActiveDirectory,FirstName,LastName,Type,Status,ModifierId,Date)
SELECT [Id], [ActiveDirectory], [FirstName], [LastName], [Type], [Status], [dbo].GetUserContext(), GETDATE()
FROM [dbo].[Users] WHERE [Id] = #UserId
INSERT INTO [Audit_UsersAddresses]([User], [Address])
SELECT ##IDENTITY, [Address]
FROM [UsersAddresses]
WHERE [User] = #UserId;
END;
GetUserContext() that is used in the insert :
CREATE FUNCTION [dbo].[GetUserContext] ()
RETURNS INT
AS
BEGIN
RETURN COALESCE(CONVERT(INT, CONTEXT_INFO()), 0)
END
And Finally the procedure to set the context.
CREATE PROCEDURE [dbo].[SetUserContext]
#userId INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE #context VARBINARY(128)
SET #context = CONVERT(BINARY(128), #userId)
SET CONTEXT_INFO #context
END

Creating a status update trigger

I have 2 tables like so:
JOBS table
Jobcode UserId Status
101 130 R
102 139 D
USERS table
UserId Email
130 test#example.com
I want to create a trigger on insert and update that sends an email to my stored procedure:
EXEC dbo.SendMyEmail #email, #jobcode;
when the jobcode is inserted as 'D' or updated to 'D'.
In my opinion, sending email in a trigger is not optimal.
Instead, you should just insert to a queue table, and have a process run frequently that checks the table and sends the email.
What happens if you get an error in your email procedure? It will force a rollback of your job completion status. Only you know whether that is minor or possibly catastrophic. But I can tell you for sure that DB best practice is to NOT do extended I/O during a DML operation.
CREATE TRIGGER TR_Jobs_EnqueueEmail_IU ON dbo.Jobs FOR INSERT, UPDATE
AS
SET NOCOUNT ON;
INSERT dbo.EmailQueue (UserID, JobCode)
SELECT UserID, JobCode
FROM
Inserted I
LEFT JOIN Deleted D
ON I.JobCode = D.JobCode -- or proper PK columns
WHERE
IsNull(D.Status, 'R') <> 'D'
AND I.Status = 'D';
Tables needed:
CREATE TABLE dbo.EmailQueue (
QueuedDate datetime NOT NULL
CONSTRAINT DF_EmailQueue_QeueueDate DEFAULT (GetDate()),
UserID int NOT NULL,
JobCode int NOT NULL,
CONSTRAINT PK_EmailQueue PRIMARY KEY CLUSTERED (QueuedDate, UserID, JobCode)
);
CREATE TABLE dbo.EmailSent (
SentDate datetime NOT NULL
CONSTRAINT DF_EmailSent_SentDate DEFAULT (GetDate()),
QueuedDate datetime NOT NULL,
UserID int NOT NULL,
JobCode int NOT NULL,
CONSTRAINT PK_EmailSent PRIMARY KEY CLUSTERED (SentDate, QueuedDate, UserID, JobCode)
);
Then, run the following stored procedure once a minute from a SQL Job:
CREATE PROCEDURE dbo.EmailProcess
AS
DECLARE #Email TABLE (
QueuedDate datetime,
UserID int,
JobCode int
);
DECLARE
#EmailAddress nvarchar(255),
#JobCode int;
WHILE 1 = 1 BEGIN
DELETE TOP 1 Q.*
OUTPUT Inserted.QueuedDate, Inserted.UserID, Inserted.JobCode
INTO #Email (QueuedDate, UserID, JobCode)
FROM dbo.EmailQueue Q WITH (UPDLOCK, ROWLOCK, READPAST)
ORDER BY QueuedDate;
IF ##RowCount = 0 RETURN;
SELECT #EmailAddress = U.EmailAddress, #JobCode = E.JobCode
FROM
#Email E
INNER JOIN dbo.User U
ON E.UserID = U.UserID;
EXEC dbo.SendMyEmail #EmailAddress, #JobCode;
DELETE E
OUTPUT QueuedDate, UserID, JobCode
INTO dbo.EmailSent (QueuedDate, UserID, JobCode)
FROM #Email E;
END;
The delete pattern and locks I used are very specifically chosen. If you change them or change the delete pattern in any way it is almost certain you will break it. Handling locks and concurrency is hard. Don't change it.
Note: I typed all the above without checking anything on a SQL Server. It is likely there are typos. Please forgive any.
I'm not sure about data types etc but this should at least put you on the right track.
Hope it helps...
CREATE TRIGGER SendEmailOnStatusD
ON JOBS
-- trigger is fired when an update is made for the table
FOR UPDATE --You can add the same for INSERT
AS
-- holds the UserID so we know which Customer was updated
DECLARE #UserID int
DECLARE #JobCode int
SELECT #UserID = UserId, #JobCode = JobCode
FROM INSERTED WHERE [Status] = 'D' --If you want the old value before the update, use 'deleted' table instead of 'inserted' table
IF (#UserID IS NOT NULL)
BEGIN
-- holds the email
DECLARE #email varchar(250)
SELECT #email = Email FROM USERS WHERE UserId = #UserID
EXEC SendMyEmail (#email, #jobcode);
END
GO
EDIT:
Above code does not handle multiple updates, so for better practice see below option
CREATE TRIGGER SendEmailOnStatusD ON JOBS
-- trigger is fired when an update is made for the table
FOR UPDATE --You can add the same for INSERT
AS
DECLARE #Updates table(UserID int, JobCode int, Email varchar(250))
INSERT INTO #Updates (UserID, JobCode, Email)
SELECT i.UserID, i.JobCode, u.Email
FROM INSERTED i
JOIN USERS u ON i.UserID = u.UserID
WHERE [Status] = 'D'
DECLARE #UserID int
DECLARE #JobCode int
DECLARE #Email varchar(250)
WHILE EXISTS(SELECT * FROM #Updates)
BEGIN
SELECT TOP 1
#UserID = UserID,
#Email = Email,
#JobCode = JobCode
FROM #Updates WHERE UserID = #UserID
EXEC SendMyEmail (#email, #jobcode);
DELETE FROM #Updates
WHERE UserID = #UserID
END
GO
Additionally, as discussed in the comments, sending emails from a trigger is also not the best, but as this is what the question asks for it has been included. I would recommend alternative options for sending emails such as a queue which has been mentioned in other answers.

How to check for duplicate records in table while inserting and updating

I need to check for duplicate records in my table while insert and update. Below is my query. It works fine in case of insert, but fails at update. I am unable to figure that out. Please help me.
Insert:
Set #Count = (Select ISNULL(Count(*),0) From AccountMaster Where [Name] = #Name and Id=#Id and Flag=1);
If #Count >0 and #InsertUpdateFlag=1
BEGIN
Set #DbErrormessage = 'Business Name and Account Id must be unique. Try again.';
END
Update :
Set #Count = (Select Count(*) From AccountMaster Where [Name] = #Name and Id <> #Id and Flag=1);
If #Count >0 and #InsertUpdateFlag=2
BEGIN
Set #DbErrormessage = 'Business Name is in already in use by some other account. Try another one.';
END
The condition of update is that the Id and Name must not exist in the database. So I am taking count where name doesnot exists with any other id. But it seems , not working.
Below is my table schema.
SELECT [PkId] //Primary Key as Int
,[Id] // Unique Key varchar(25)
,[Created]
,[Type]
,[Status]
,[Name] //Business Name
,[ContactPerson]
,[ContactNumber]
,[Email]
,[Address]
,[LocationId]
,[Remarks]
,[Flag]
FROM [AccountMaster]
Id and Business Name must be unique while both insert/update. So I need to check it while insert and update.
Put the Unique constraint in the name and forget the checks, you can handle the Constraint violation and then you know the name already exists

SQL Triggers - how do I get the updated value?

How do I get the value of the updated record in a SQL trigger - something like this:
CREATE TRIGGER TR_UpdateNew
ON Users
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
EXEC UpdateProfile (SELECT UserId FROM updated AS U);
END
GO
Obviously this doesn't work, but you can see what I am trying to get at.
Provide you are certain that only one value will ever be updated, you can do this...
CREATE TRIGGER TR_UpdateNew
ON Users
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE #user_id INT
SELECT
#user_id = inserted.UserID
FROM
inserted
INNER JOIN
deleted
ON inserted.PrimaryKey = deleted.PrimaryKey
-- It's an update if the record is in BOTH inserted AND deleted
EXEC UpdateProfile #user_id;
END
GO
If multiple values can be updated at once, only one of them will get processed by this code. (Although it won't error.)
You could use a cursor, or if it's SQL Server 2008+ you can use table variables.
Or, more commonly, just move the StoredProcedure code into the trigger.
Based on my knowledge you would need to create a CURSOR to loop through all the updated values to execute the UpdateProfile procedure. Keep in mind this will slow down your update process.
Declare #UserID int --Assuming
Declare UpdateProfile_Cursor Cursor for Select UserID From inserted;
Open Cursor UpdateProfile_Cursor;
Fetch Next from UpdateProfile_Cursor Into #UserID;
While ##FETCH_STATUS == 0
Begin
Exec UpdateProfile #UserID
Fetch Next from UpdateProfile_Cursor Into #UserID;
End
CLOSE UpdateProfile_Cursor
DEALLOCATE UpdateProfile_Cursor
My syntax may be a little off but this will give you the desired effect. Again, consider revising your logic to handle multiple updates as using cursors is resource intensive.
You can do something like this example where I'm logging changes to a transaction history table:
create table dbo.action
(
id int not null primary key ,
description varchar(32) not null unique ,
)
go
insert dbo.action values( 1 , 'insert' )
insert dbo.action values( 2 , 'update' )
insert dbo.action values( 3 , 'delete' )
go
create table dbo.foo
(
id int not null identity(1,1) primary key ,
value varchar(200) not null unique ,
)
go
create table dbo.foo_history
(
id int not null ,
seq int not null identity(1,1) ,
action_date datetime not null default(current_timestamp) ,
action_id int not null foreign key references dbo.action ( id ),
old_value varchar(200) null ,
new_value varchar(200) null ,
primary key nonclustered ( id , seq ) ,
)
go
create trigger foo_update_01 on dbo.foo for insert, update , delete
as
set nocount on
set xact_abort on
set ansi_nulls on
set concat_null_yields_null on
--
-- record change history
--
insert dbo.foo_history
(
id ,
action_id ,
old_value ,
new_value
)
select id = coalesce( i.id , d.id ) ,
action_id = case
when i.id is not null and d.id is null then 1 -- insert
when i.id is not null and d.id is not null then 2 -- update
when i.id is null and d.id is not null then 3 -- delete
end ,
old_value = d.value ,
new_value = i.value
from inserted i
full join deleted d on d.id = i.id
go
But you can use the same sort of technique, mix it up a bit and pass the entire set of values to a stored procedure, like I do in the following example (using the table schema above).
First, create a stored procedure that expects a particular temp table to exist at runtime, thus:
--
-- temp table must exist or the stored procedure won't compile
--
create table #foo_changes
(
id int not null primary key clustered ,
action_id int not null ,
old_value varchar(200) null ,
new_value varchar(200) null ,
)
go
--
-- create the stored procedure
--
create procedure dbo.foo_changed
as
--
-- do something useful involving the contents of #foo_changes here
--
select * from #foo_changes
return 0
go
--
-- drop the temp table
--
drop table #foo_changes
go
Once you've done that, create a trigger that will create and populate the temp table expected by the stored procedure and then execute the stored procedure:
create trigger foo_trigger_01 on dbo.foo for insert, update , delete
as
set nocount on
set xact_abort on
set ansi_nulls on
set concat_null_yields_null on
--
-- create the temp table. This temp table will be in scope for any stored
-- procedure executed by this trigger. It will be automagickally dropped
-- when trigger execution is complete.
--
-- Any changes made to this table by a stored procedure — inserts,
-- deletes or updates are, of course, visible to the trigger upon return
-- from the stored procedure.
--
create table #foo_changes
(
id int not null primary key clustered ,
action_id int not null ,
old_value varchar(200) null ,
new_value varchar(200) null ,
)
--
-- populate the temp table
--
insert #foo_changes
(
id ,
action_id ,
old_value ,
new_value
)
select id = coalesce( i.id , d.id ) ,
action_id = case
when i.id is not null and d.id is null then 1 -- insert
when i.id is not null and d.id is not null then 2 -- update
when i.id is null and d.id is not null then 3 -- delete
end ,
old_value = d.value ,
new_value = i.value
from inserted i
full join deleted d on d.id = i.id
--
-- execute the stored procedure. The temp table created above is in scope
-- for the stored procedure, so it's able to access the set of changes from
-- the trigger.
--
exec dbo.foo_changed
go
That's about all there is to it. It's simple, it's easy, it works for change sets of any size. And, it's safe, with no race conditions or collisions with other users in the system.