Can sql server queries be really cancelled/killed?

后端 未结 2 1665
粉色の甜心
粉色の甜心 2020-12-16 15:13

I would like to give a user the ability to cancel a running query. The query is really slow. (Query optimization is besides the point.) This is mainly out of my curiosity.

相关标签:
2条回答
  • 2020-12-16 16:10

    Yes, you can kill a process from .NET. Here is an example. Please note you will need proper permissions and you have to figure out the process in question. I don't have a quick sample of determining which process your query is running under.

    You example aborts the thread, but that does not mean the work on SQL Server was terminated. If you think about it this way: when you go through a bad cell zone and the call drops, if you mom/wife/friend was droning on, do they instantly stop talking? That is an analogy of aborting the thread, at least in the case of working with a database server.

    0 讨论(0)
  • 2020-12-16 16:19

    IF you really absolutely want to kill it for good use this approach:

    • store away the session ID right before starting the long-running query by calling SELECT @@SPID AS 'SESSIONID' on the same connection

    When you want to kill it:

    • Open a new DB connection
    • issue a KILL command for that session ID
      BEWARE as the MSDN documentation states you need the permission ALTER ANY CONNECTION to do this
    0 讨论(0)
提交回复
热议问题