Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

SQL 2012 trigger inexplicably not executing when table modified by another trigger

$
0
0

I have a table with an after-trigger with code to update a summary field:

CREATE TRIGGER [dbo].[Service_Update]
   ON  [dbo].[Service]
   AFTER UPDATE
AS 
BEGIN
	SET NOCOUNT ON;
	IF Trigger_NestLevel() >= 2 RETURN

	IF EXISTS (SELECT 1 FROM inserted)
	BEGIN
		MERGE dbo.Service AS target
		-- select-statement simplified for brevity
		USING (SELECT i.UID, i.JobCode + ' ' + i.City + ': ' + Count(od.OrderUID) AS Summary
			FROM inserted i
			INNER JOIN dbo.Service s ON i.UID = s.UID
			INNER JOIN dbo.OrderDetail od ON i.UID = od.OrderUID
			GROUP BY od.OrderUID)
			AS source (UID, Summary)
			ON (target.UID = source.UID)
			WHEN MATCHED THEN
				UPDATE SET Summary = source.Summary;
	END
END


The Service_Update trigger works as expected when changes are made to records in the Service table.

When changes are made to a child table, dbo.OrderDetail, such that the number of records linked to the parent table, dbo.Service, changes, I want to have the trigger on the Service table execute to update the summary line.

This trigger is on the OrderDetail table:

CREATE TRIGGER OrderDetail_Update
   ON dbo.OrderDetail
   AFTER INSERT, UPDATE, DELETE
AS 
BEGIN
	-- Touch the Service table to cause the summary line to  -- get updated if a record has been added or removed. 
	MERGE dbo.Service AS target
	USING (SELECT OrderUID FROM inserted
		GROUP BY OrderUID
	UNION SELECT OrderUID FROM deleted
		GROUP BY OrderUID) AS source (OrderUID)
	ON (target.UID = source.OrderUID)
	WHEN MATCHED THEN
		UPDATE SET Note = Note;
END

When changes are made to the OrderDetail table, the OrderDetail_Update trigger executes. This trigger makes trivial updates to the associated records in the Service table. Unfortunately, even though the records in the Service table get updated, the trigger on the Service table does not execute.

I have confirmed that the code in the OrderDetail_Update trigger is being executed. If I manually execute equivalent commands (I can't execute the actual commands because the inserted and deleted tables only exist within the trigger) the Service_Update trigger executes as expected.

I thought that the issue might be that SQL Server is smart enough to figure out that setting a field to its existing value does not actually change anything and hence SQL Server skips the operation. However, as a test I tried changing the update-command to say, "SET Note = IIF(Note is null, '', null)" to ensure that the value gets set to something different but it did not fix things.

Is there some reason why changes made to a table originating from code executing in a trigger would not cause a trigger on the table to fire? Is there any other obvious reason why the above scheme would not work?

I am running SQL Server 2012 SP1 Standard Edition, 64-bit.


Viewing all articles
Browse latest Browse all 12963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>