| Author |
Topic |
|
Chainwhip
Starting Member
33 Posts |
Posted - 2008-06-18 : 16:23:19
|
| Hi. Apologies if this has come up before, I wonder if someone can help.I want to call a sproc from a trigger, passing in the value of a field from the updated/inserted/deleted row.It would be like spUpdateUser @UserID = xwhere x is the value from one of the columns of updated row. What is the syntax for this please? TIA |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-18 : 16:30:58
|
| The trigger tables (inserted and deleted) could contain multiple rows, so you can not call your stored procedure without looping through the table. Can the logic of spUpdateUser be changed to handle multiple rows?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
Chainwhip
Starting Member
33 Posts |
Posted - 2008-06-18 : 16:37:49
|
| thanks tara, I didn't realise there were trigger tables involved - it's something i haven't done before. what would they contain multiple rows of? The sproc being called will be updating a value in a different table altogether by calling a function, but it needs the UserID from the row that has just been altered. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Chainwhip
Starting Member
33 Posts |
Posted - 2008-06-18 : 17:02:37
|
| ah gotcha! i may be updating or deleting 3 rows in the triggered table so i need to call the sproc 3 times, one for each row affected. i think therefore i'll need to create a cursor to do this, as the sproc being called needs to be run for each row - unless there's a clever way of wrapping the call in a 'set-based solution'? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-18 : 17:06:13
|
| The clever way is to not call a stored procedure for each row, instead rewrite the logic to handle multiple rows. Could you post the code for the stored procedure so that we can see if the logic can be rewritten into the trigger set-based?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
Chainwhip
Starting Member
33 Posts |
Posted - 2008-06-18 : 17:14:04
|
| Sure, the sproc to be called is this. The function it calls does some calculations on other tables to return an int representing progress ALTER PROCEDURE [dbo].[sp_Cache_UserProgress] @UserID INTASBEGIN SET NOCOUNT ON; DECLARE @Progress INT SELECT @Progress = Progress FROM dbo.func_GetProgress_User(NULL, NULL, @UserID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 8, NULL, NULL) IF NOT EXISTS(SELECT UserID FROM Cache_UserProgress WHERE UserID = @UserID) INSERT Cache_UserProgress (UserID, Progress, datCache) VALUES (@UserID, @Progress, GETDATE()) ELSE UPDATE Cache_UserProgress SET Progress = @Progress, datCache=GETDATE() WHERE UserID = @UserIDEND |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-18 : 17:17:19
|
| We'd need to rewrite the function as well, but here's a start on your trigger for the UPDATE portion of your stored procedure. So you'd do this instead of calling a stored procedure.UPDATE cSET Progress = @Progress, datCache = GETDATE()FROM Cache_UserProgress cINNER JOIN inserted iON c.UserID = i.UserIDTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
Chainwhip
Starting Member
33 Posts |
Posted - 2008-06-18 : 17:25:16
|
| The function is very long and complicated so I don't want to change it - it will mean days of retesting the application!In your opinion will there be any significant 'punishment' by using a cursor in the trigger? we're getting about 10 inserts/updates every minute at peak time |
 |
|
|
|