I want to call stored procedure from a trigger,
how to execute that stored procedure after x minutes?
I’m looking for something other than WAITFOR DELAY
thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Have an SQL Agent job that runs regularly and pulls stored procedure parameters from a table – the rows should indicate also when their run of the stored procedure should occur, so the SQL Agent job will only pick rows that are due/slightly overdue. It should delete the rows or mark them after calling the stored procedure.
Then, in the trigger, just insert a new row into this same table.
You do not want to be putting anything in a trigger that will affect the execution of the original transaction in any way – you definitely don’t want to be causing any delays, or interacting with anything outside of the same database.
E.g., if the stored procedure is
Then we’d create a table:
And the SQL Agent job would do:
And the trigger would just have:
If you’re running in an edition that doesn’t support agent, then you may have to fake it. What I’ve done in the past is to create a stored procedure that contains the “poor mans agent jobs”, something like:
Then, create a second stored procedure, this time in the
masterdatabase, which waits 30 seconds and then calls the first procedure:And then, mark this procedure as a startup procedure, using
sp_procoption:And restart the service – you’ll now have a continuously running query.