I would like to be able to abort a specific sql statement if it takes longer than a specified number of seconds. How can I do that?
For example, I would like to do something like:
SET NextStatementTimeOutSeconds = 60
SELECT * FROM MyTable
IF @@LastStatementTimedOut = 1
PRINT 'Statement timed out after 60 seconds'
ELSE
PRINT 'Statement completed in less than 60 seconds'
Please note that I made up NextStatementTimeOutSeconds and @@LastStatementTimedOut in order to illustrate what I would like to do.
Thanks for the suggestions. I’m thinking about using such a timeout inside a single sql script/batch like the one above, if possible.
Background: We have several stored procedures that I’d like to be faster than X seconds. So I’d call them one after each other; and if one takes more than X seconds, I’d just abort and print a statement like ‘sproc 13 takes too long’. The data varies and I’m trying different index changes, so I’d like to time-test all sprocs after such changes in order to make sure that all of them are below X seconds.
Typically the timeout is set on the command that issues the SQL statement. If that’s in C# for example, then you set the IDbCommand.CommandTimeout property and it’ll throw a SQLException after the timeout expires if the statement hasn’t completed. This will then rollback any transactions.
In scheduled tasks on SQL Server, I’d expect that when you configure the job, there will be a value in an up/down control somewhere that allows you to change the timeout of the job. I think the terminology in SQL Server is a “Execution Timeout” but I don’t know how to set it I’m afraid.
The point is – look at the thing running the statement, rather than the actual statement itself.