access_time 13 years ago language中文
more_vert

因为数据库正在使用,所以未能获得对数据库的排它访问权 解决办法

visibility 394 comment 0

恢复数据库的时候遇到这个错误

解决办法

/*
断开所有用户打开的连接
*/
use master
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_killspid]
GO

create proc p_killspid
@dbname sysname --要关闭进程的数据库名
as
declare @s nvarchar(1000)
declare tb cursor local for
select s='kill '+cast(spid as varchar)
from master..sysprocesses
where dbid=db_id(@dbname)

open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
go

--用法
exec p_killspid 'hj'

--恢复数据库.
RESTORE DATABASE hj FROM disk='D:\hj.dat'

info Last modified by Raymond 7 years ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn.

Want to contribute on Kontext to help others?

Learn more

More from Kontext

visibility 433
thumb_up 0
access_time 8 years ago
visibility 1029
thumb_up 0
access_time 9 years ago
visibility 5792
thumb_up 2
access_time 8 years ago