How to stop a running query?

前端 未结 4 1739
被撕碎了的回忆
被撕碎了的回忆 2021-02-18 14:08

I use RODBC to send queries to an SQL-Server. Sometimes they take too much time to run, so I need to cancel them.

Clicking the red \"stop\" button in RStudio yields this

4条回答
  •  有刺的猬
    2021-02-18 14:51

    Most SQL Server users use SQL Server Management Studio (which is free and can be downloaded from Microsoft) to connect to SQL Server or execute commands from the command line via a tool called SQLCMD.

    If you can determine the session id that the SQL Command is being run in you can kill the session which would stop any executing command(s). SQL Server will still need time (could be a 'long' time) to rollback any changes made during the execution of the command.

    Terminating a session (depending on the software) can take a while to communicate to SQL Server that the session has been terminated. When I connected to DB2 from SQL Server using linked servers DB2 would buffer the terminate command and it would frequently take up to an hour for DB2 to realize the session had been terminated.

    To determine what the session you are running in you can try:

    select @@spid;
    

    once you have the spid (lets say 86) you can then issue (depending on if you have permission to do so)

    kill 86;
    

    but as Microsoft notes: Terminates a user process that is based on the session ID or unit of work (UOW). If the specified session ID or UOW has a lot of work to undo, the KILL statement may take some time to complete, particularly when it involves rolling back a long transaction.

提交回复
热议问题