Setting query timeout on a stored procedure in SQL Server 2005 - sql

Does anyone know how to set the timeout on a stored procedure? Found some examples on the NET, e.g sp_configure 'remote Query Timeout', 5, but this did not work. Also found some commands "DBPROP_COMMANDTIMEOUT" and "DBPROP_GENERALTIMEOUT" but i don't know if they are the right ones to use and if they are, how to use them in my transact-SQL code.

As Chris Tybur said, you can not the the query timeout for a stored proc in the stored proc or on the SQL Server.
CommandTimeout is a client concept: the client will abort the query after a certain amount of time. There is no dead man's timer or mechanism for a stored proc to abort itself /or any query). SQL server will allow a query to run forever.
The "Remote Query Timeout" is exactly that: timeout when SQL Server makes a remote call, when SQL Server itself is the client of another server. It says in the description:
This value applies to an outgoing
connection initiated by the Database
Engine as a remote query. This value
has no effect on queries received by
the Database Engine.
A recent question with good info: timeout setting for SQL Server

I have never heard of setting a timeout for executing a stored procedure on the server side. Usually you would specify the timeout for the command that runs the procedure in the data provider you are using, such as ADO.NET.

Wait - the real question is, "What is happening that you want to prevent?" Everyone has focused on server-side, client-side but in truth we don't know why you are asking this question (and it's important).
And another "why": why do you want to set a timeout on a "stored procedure"? Why not a view, a function, or a query? Did you use the term "stored procedure" for a particular reason or would you just be interested in learning how to set a timeout in T-SQL?
I'm asking because I wonder if you are having locking issues and perhaps SET LOCK_TIMEOUT 1000 or WITH(NOLOCK) might be what you really need. Without more info though I can't say. If you can give us more feedback on why you are asking, what is happening, and what ultimately you want to have happen if your "timeout" is reached, maybe we can help more.
Bottom line: Yes, you can set a timeout in T-SQL and yes you can stop the execution of a stored procedure with T-SQL. But I don't know enough about what you want to offer advice on where to look or to give you more info. I'm kinda scared that I've already said too much :)

This article has a very good explanation on query timeouts and how they're a client-side concept only. You can set the query timeout in SQL Server management studio from the Tools|Options menu.

You have to set the timeout when you execute the stored procedure on the client. As far as the SQL Server goes, it'll let the stored procedure run for ever unless told to cancel it.

Related

SQL Server stored procedure or alternative method to restart SQL Server Agent

There are various ways to restart the SQL Server Agent on a server, but I would like to do it from a stored procedure in one of my databases (on the same server). How would one go about doing that? Is there some sort of a system stored procedure that I could call? Or would I need to call some sort of third party library/external language to accomplish that such as the following?
Apparently one of my co-workers had already solved this:
EXEC xp_servicecontrol N'STOP',N'SQLServerAGENT';
-- Give the service a little time to stop
waitfor delay '00:00:10.000'
EXEC xp_servicecontrol N'START',N'SQLServerAGENT';
Not sure this the best solution, but should work for my purposes...
PowerShell could be a good approach depending on the circumstances. the Start-Service and Restart-Service commands would be a good starting point :O

SQL Server Stored Procedure RPC VS SSMS

I have a stored procedure that takes 1 parameter. When I run the stored procedure from SQL Server Management Studio, it runs in 2-4 seconds. When I call it with a console application, it takes 30+ seconds. The SQL Server is remote and both SSMS and my application are being run from my local machine so I don't think it's a networking issue.
I've ran the SQL Server Profiler to try to track down the issue and one thing I'm seeing is that when it's run from SSMS it starts the statement, recompiles it, then starts it over again, then completes it, like this:
SP:StmtStarting
SP:Recompile
SQL:StmtRecompile
SP:StmtStarting
SP:StmtCompleted
The 2 recompile entries have an EventSubClass of "2 - Statistics changed"
From the app I only see entries for SP:StmtStarting & SP:StmtCompleted, no recompile entries.
I'm calling exactly the same stored procedure with the same parameter value. Why does SSMS recompile based on statistics but my console app does not?
After researching and troubleshooting it appears to be entirely due to SET_ARITHABORT_ON. SSMS defaults this to 'ON' while the .net sql client defaults it to 'OFF' so it was going with 2 different execution plans, although I'm not entirely sure why the two plans are so drastically different.
I overrode the OpenConnection() method to open the connection set it to ON and my application then had the same performance as SSMS. I hope this helps anyone else who stumbles upon this.

Sudden degradation in LINQ to SQL stored procedure performance from ASP.NET site, including timeouts

The site is ASP.NET 2.0, using LINQ to SQL. Database is SQL Server 2008 R2.
Been working on an issue where performance suddenly took a huge drop one day and has remained that way since. Cannot figure out why. It has been just certain functionality of the site, not necessarily a site-wide problem. Have focused on a particular stored procedure in general that is taking a good 1000ms+ showing in profiler. When copying the TextData and running right in the query analyzer, it runs much quicker.
Have tried a sp_recompile on the stored procedure as well as the table used. The db server was restarted during a maintenance period and that also did not stabilize things. Is there any possible troubleshooting steps anyone could provide to help dig deeper on this? Absolutely stumped.
It could be a parameter sniffing problem. You can see the details here - http://blogs.msdn.com/b/turgays/archive/2013/09/10/parameter-sniffing-problem-and-workarounds. As suggested in the blog, you can try the following -
OPTION (OPTIMIZE FOR (#VARIABLE=VALUE))
OPTION (OPTIMIZE FOR (#VARIABLE UNKNOWN))
Use local variables - Basically declare local variables and assign the value of the parameter to local variable and use the local variable in the query.
OPTION (RECOMPILE) - Since you have already tried that you can ignore this
Could you post the query snippet which is causing this issue?

Configure MS SQL Server to log query and its execution time

I had a program using SQLconnector to connect to MS SQL 2012, I want know SQL query execution time. I am aware there is a SQL Server Profiler, but I can't configure it correctly to capture any query execution time. I am also aware that I can add timer within program but i can not change code easily.
All advice welcome. thanks
AFAIK SQLConnector is for MySQL, so I don't understand how you can work with SQLServer 2012.
If you need profiling in SQLServer - there is nothing better than SQL Server Profiler.
I can't configure it correctly to capture any query execution time
It must be easy:
Event Selecion - you need SQL:Stmt Completed (uncheck everything else). You need to select duration.
Column Filters - create filter by Login Name (make special login if you need it), TextData, Spid (if you can figure out how to obtain it)
This is not supported by default but you can do this using several techniques depending on what are your needs.
Do you need to capture every statement including SELECT? If yes then I suggest you still use SQL Server Profiler or SQL Server Traces
If you only need to capture DML statements (excluding SELECT) you can try setting up triggers on tables you want to audit.
There are also DDL triggers that can help you catch DDL statements (as far as I know there is no way to capture all DDL statements using DDL triggers).
So, there are many options here but it all depends on what are your needs.

How to detect whether a stored procedure is being run in SQL Server 2008 R2

Is there a view or internal sp to do this?
For example, I have a sp spGoesOnForSomeTime.
If I kicked this off then some individual closes my computer down, how can I see whether this is still running or not?
I realise I can use SQL Profiler and ActivityMonitor but I ultimately want to relay this information back through a web app.
EDIT: Apologies, it is not a local connection.
If it's run under your local connection, then it will stop and roll back, so you can be confident that it is not running.
If it's running under the context of another connection, you can use the sp_who stored procedure to see all of the activity (and active connections) on the server, and the cmd column should provide you with the command. If any of the records have your procedure name in their cmd column, then that will tell you that it's executing.
You may, however, want to take a more intentional approach and set a flag of some kind (a value in a row in another table, an extended property on the database or procedure, etc.) when the procedure starts, then reset it when the procedure finishes. This would also account for scenarios where your procedure gets called from within another procedure.
If running it from your local Management Studio, the connection will be broken (closed) when SSMS closes.
Any transactions will rollback, all locks will be released. If you're in the middle of some huge data changes, your proc stops running and the connection is still closed, but the rollback will continue anyway
So no need to check based on the facts given...
Depends whether you're running it using SqlServerAgent or just via Mgmt Studio.
As #gbn said, once you disconnect then any work using that connection will stop too.
I'd say create a scheduled Job and then it's independent of your connection.
If this is part of an application requirement then I would log the start and finish of all of the calls to this stored procedure then have a view against that to show which instances of the stored procedure are currently running.
If this is for maintenance of the database then I would use sp_who or sp_who2.
EDIT:
Actually, sp_who and sp_who2 will likely show you the currently running command, not necessarily the stored procedure name itself. You can try using the system dynamic view sys.dm_exec_sql_text, but that isn't always going to be accurate either.