图三
我们可以从结果(见图三)看到,执行指令后成功终止了SPID 54。当试图终止57时失败了。同时结果也显示了为什么没能终止特定SPID的信息。
用法二
下面,假设我们有51、52、53、54、55、57、58、59和60这几个SPID,而我们的目标是结束SPID从25到70的进程。
执行以下命令:
use master go kill2 '25-75' go |
运行结果:
Killing all SPIDs from 25 to 75 Cannot kill the SPID 25 because it does not Exist ….. Cannot kill the SPID 48 because it does not Exist Cannot kill the SPID 49 because it does not Exist Cannot kill the SPID 50 because it does not Exist Killing 51 Killing 52 Killing 53 Killing 54 Killing 55 Cannot kill the SPID 56 because it does not Exist Killing 57 Msg 6104, Level 16, State 1, Line 1 Cannot use KILL to kill your own process. Killing 58 Killing 59 Killing 60 Cannot kill the SPID 61 because it does not Exist ..... Cannot kill the SPID 75 because it does not Exist |
图四
从结果(见图四)我们可以看到“KILL2”存储过程忽略了所有SPID小于50的连接,而结束了从51到70的所有进程。
用法三
接下来,假设我们要终结掉所有连接到数据库AdventureWorks2008的会话,同时又假设SPID为53、54、58和60的进程连接到了该数据库(见图五)。
图五
现在,我们执行以下的T-SQL语句结束掉所有这些会话。
Use master go kill2 'db=AdventureWorks2008' go |
运行结果:
Killing all the SPIDs that are connected to the database AdventureWorks2008 Killing 53 Killing 54 Killing 58 Killing 60 |
图六
从结果(见图六)我们可以看到“KILL2”存储过程终止了所有连接到AdventureWorks2008数据库的会话。
用法四
“KILL2”存储过程的第四种用法类似于“KILL命令,也就是一次解决一个会话,如下所示:
Use master go kill2 '56' go |