[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cancel MS SQL Query

Posted on 2008-11-10
18
Medium Priority
?
1,247 Views
Last Modified: 2012-05-05
I wanted to please ask how can I cancel a query in MS SQL if the query takes more than 3 minutes to run.  I need to place this logic in the query itself that is used.  I am using MS SQL 2005.  Thanks!
0
Comment
Question by:jjrr007
  • 7
  • 6
  • 4
17 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22921867
AFAIK, you can't put that logic in the query itself.

You CAN put it into the application that's call the SQL Server w/ the query.  That would be the CommandTimeout property (in seconds) in either ADO or ADO.Net.

There's also a setting at the server level ... but for most application's that's WAY too broad.
0
 
LVL 1

Author Comment

by:jjrr007
ID: 22922365
How about adding the logic to the MS Sql server job?The query is executed by the job.  

I am not using the query in ado/ado.net.  Thanks.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22922388
Why are you concerned about the timeout?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22922482
I can't find a timeout option in the job setup ...
0
 
LVL 1

Author Comment

by:jjrr007
ID: 22922959
How about calling the job using a bat file?  The bat file could have the time out option.  How would I do that?  
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22923027
Why are you concerned about the timeout?
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22924356
jjrr, the question chapmandew is asking is very valid.  If we know WHY, we may be able to suggest a better solution.

If you do want to do it from the command line, check out the sqlcmd utility ... and the -t option: http://msdn.microsoft.com/en-us/library/ms162773.aspx
0
 
LVL 1

Author Comment

by:jjrr007
ID: 22927357
I agree.  Chapmandew's question is a good one.  I ask because at times a query that I have written can take more than a few minutes to run.  The query can take longer for a wide variety of circumstances.  For example, the server may have too much traffic or there could be some tables that are updating etc.  

When the query takes more than a few minutes to run, I have noticed that the query (that has been optimized) can negatively impact the server.  Since I am running the query every 25 minutes, I want to the query to stop running when it is taking more than 3 minutes to run.  What do you suggest?  Thanks!
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22933168
I can't think of an option better than using SqlCmd to execute it from the command-line and using the -t option.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22933218
In that case, you might be better off querying system tables to see if you have any active queries that have been open for a certain amt of time...and cancel it.  Not a good idea, but an option.
0
 
LVL 1

Author Comment

by:jjrr007
ID: 22944872
Thanks.  Maybe I am mistaken, but I don't think this will cancel the query.  From what I know this will only stop the process if it is not executed. In other words, the T option is used to monitor the time until it is executed. Is that right?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22946201
are you referring to my post or Daniel's?
0
 
LVL 1

Author Comment

by:jjrr007
ID: 22951621
chapmandew, Thanks for your time.  In my last post, I was eferring to Daniel's post regarding the t option.  I apologize for not looking more closely at your post.  

 How would I write a query that you mentioned in your post "22933218"? There is only one stored procedure that is doing this so I could just look for that.  
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 22952315
this should give you the session id of anything that has been executing for longer than 60 seconds.

select session_id
from
(
select objectname = object_name(rr.objectid, rr.dbid), * from sys.dm_exec_requests r
cross apply sys.dm_exec_sql_text(sql_handle) rr
where session_id > 50 and datediff(s, start_time, getdate()) > 60
) a
where objectname = 'yourstoredprocname'
0
 
LVL 1

Author Comment

by:jjrr007
ID: 22953021
Thanks.  This looks good Chapnew.  How do I tell the server to cancel the query?
0
 
LVL 1

Author Comment

by:jjrr007
ID: 24307724
I appreciate everyone's time.  I really just need to know how to cancel the query.  I haven't received an answer about cancelling a query..
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 2000 total points
ID: 24307767
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question