如何以编程方式取消 SQL Server 执行过程 [英] How can you cancel a SQL Server execution process programmatically

查看:49
本文介绍了如何以编程方式取消 SQL Server 执行过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您从代码中执行了以下(长时间运行)过程:

Let's say you have execute the following (long running) process from your code:

int processID = DB.Execute(SQL); //some long running sql statement

有没有办法以编程方式调用 SQL Server 来取消进程,如果它花费的时间太长(有点像点击 QueryAnalyzer 中的停止"按钮)?

Is there a way to programatically call SQL Server to cancel the process if it is taking too long (kind of like hitting the "Stop" button in QueryAnalyzer)?

//cancel the process if it is taking too long
DB.Execute("sp_CancelProcess @ProcessID=" + processID);

推荐答案

use KILL 使用进程 ID:

use KILL with the process id:

KILL 53;

请注意,您不能杀死自己的 spid,您需要创建另一个连接,然后从

Just be aware that you can't kill your own spid, you need to create another connection and then kill the spid from the

如果您尝试杀死自己的 SPID,您将收到以下错误

if you try to kill your own SPID you will get the following error

Server: Msg 6104, Level 16, State 1, Line 1
Cannot use KILL to kill your own process.

这篇关于如何以编程方式取消 SQL Server 执行过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆