请教一个问题

cosmosfang 2003-09-15 12:32:55
我想要执行sp_detach_db来分离数据库,但是如果当前数据库在使用的话,是不能够detach的,所以我想知道怎么强行对数据库进行分离操作,或者有什么办法用sp来关闭到当前数据库的所有连接。
急,100分相送
...全文
29 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-09-15
  • 打赏
  • 举报
回复
declare @sql varchar(100)

while 1=1
begin

select top 1 @sql = 'kill '+cast(spid as varchar(3))
from master..sysprocesses
where spid > 50 and spid <> @@spid

if @@rowcount = 0
break

exec(@sql)
end



再做下一步
txlicenhe 2003-09-15
  • 打赏
  • 举报
回复
1:如上killspid
2:服务重启
zarge 2003-09-15
  • 打赏
  • 举报
回复
转贴

use master
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 '数据库名'
伍子V5 2003-09-15
  • 打赏
  • 举报
回复
再不在企业管理器里“管理”“当前活动”“进程信息”里kill连接到你数据库上的进程就可以
伍子V5 2003-09-15
  • 打赏
  • 举报
回复
use master
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 '数据库名'
伍子V5 2003-09-15
  • 打赏
  • 举报
回复
EXEC sp_dboption '数据库名', 'dbo use only', 'TRUE'
cosmosfang 2003-09-15
  • 打赏
  • 举报
回复
谢谢各位

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧