Can sql server queries be really cancelled/killed?

后端 未结 2 1672
粉色の甜心
粉色の甜心 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: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

提交回复
热议问题