在master下创建存储过程如下:

createproc killspid (@dbnamevarchar(20))
as
begin
[email protected](500)
[email protected]
[email protected]='declare getspid cursor for select spid from sysprocesses where dbid in (select dbid from sysdatabases where name=' [email protected]+' )'
exec(@sql)
open getspid
fetch next from getspid [email protected]
while @@fetch_status<>-1
begin
exec('kill '[email protected])
fetch next from getspid [email protected]
end
close getspid
deallocate getspid
end
--用法

use master
exec killspid '''数据库名'''

单引号嵌套

用三个单引号的作用,最外层的单引号表示har,第二个单引号是转义,最里面的单引号是字符串中的单引号;declare @dbname nvarchar(500)

set @dbname='''zcc''' --得到的变量为'zcc', set @dbname='zcc' --得到的变量是zcc