简体   繁体   中英

Trigger will not execute stored procedure

I have the following trigger:

USE SomeDB
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [Staging].[RunPivot15] 
ON [Staging].[UriData]
AFTER INSERT, UPDATE 
AS 
BEGIN
    SET NOCOUNT ON
    EXEC [Staging].[PivotData]
END

It will not fire. The table concerned receives about 30 rows every five minutes. From that point I am stuck. I have been reading that because more than one row is being inserted I have to run a cursor. I have tried the cursor and cannot get that to work either.

Can you advise what the best approach here is?

TIA

It's highly unlikely the trigger to not run. Add a couple of print statements around the procedure call in your trigger, eventually in your stored procedure too. This will help you to trace the execution when you run an update statement in Management Studio to fire the trigger.

USE SomeDB
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO    

ALTER TRIGGER [Staging].[RunPivot15] 
ON [Staging].[UriData]
AFTER INSERT, UPDATE 
AS 
BEGIN
    SET NOCOUNT ON
    PRINT 'Before call.'
    EXEC [Staging].[PivotData]
    PRINT 'After call.'
END

Then run an update statement in Management Studio and check Messages tab to see is your messages printed.

update Staging.RunPivot15 set SomeColumn = SomeColumn where SomeColumn = SomeValue

No, you do not need cursors. When your trigger is executed, if more than one row is affected, there will be multiple rows in inserted / deleted pseudo tables too. In your case you do not read which rows are updated either, so just run the procedure. If you need to know which rows exactly are modified, then write code to process them in set-based approach (all rows at once). Looping with cursors is practically never good idea in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM