/*
关闭用户打开的进程,再分离即可
*/
use master
goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_killspid]
GOcreate proc p_killspid
@dbname varchar(200) --要关闭进程的数据库名
as
declare @sql nvarchar(500)
declare @spid nvarchar(20) declare #tb cursor for
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
open #tb
fetch next from #tb into @spid
while @@fetch_status=0
begin
exec('kill '+@spid)
fetch next from #tb into @spid
end
close #tb
deallocate #tb
go--用法
exec p_killspid '数据库名'
go
create proc killspid (@dbname varchar(20))
as
begin
declare @sql nvarchar(500),@temp varchar(1000)
declare @spid int
set @sql='declare getspid cursor for
select spid from sysprocesses where dbid=db_id('''+@dbname+''')'
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status<>-1
begin
set @temp='kill '+rtrim(@spid)
exec(@temp)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
end --用法
use master
exec killspid '数据库名'
sp_detach_db DataBase0
GO
你就想踩在登子上要换登子一样,你要先站到另一登子上(use master)
我的函数是:
table1->Close();
database1->Close();
执行:
exec sp_detach_db DataBase0
这个函数我调用了两次。
第一次成功分离,第二次调用汇报错
可否再帮忙解释一下?!
if db_id('你的库名') is not null
sp_detach_db DataBase0
exec sp_detach_db DataBase0