如果sql收缩数据库报出现锁的错误,那么执行一下下面的语句,再次收缩即可。
select spId
from master..SysProcesses
where db_Name(dbID) = '数据库名字'
and spId <> @@SpId
and dbID <> 0
USE master
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
go
create PROC [dbo].[Sp_KillAllProcessInDB] @DbName VARCHAR(100)
AS
IF DB_ID(@DbName) = NULL
BEGIN
PRINT 'DataBase dose not Exist'
END
ELSE
BEGIN
DECLARE @killspId VARCHAR(30)
DECLARE TmpCursor CURSOR
FOR
SELECT 'Kill ' + CONVERT(VARCHAR, spid) AS spId
FROM master..SysProcesses
WHERE DB_NAME(dbid) = @DbName
AND spid <> @@spid --终止不是当前进程的进程(不等于当前进程)
AND dbid <> 0 --数据库ID不是0
OPEN TmpCursor
FETCH NEXT FROM TmpCursor
INTO @killspId --select into @killspId
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@killspId)
FETCH NEXT FROM TmpCursor
INTO @killspId
END
CLOSE TmpCursor
DEALLOCATE TmpCursor
END
exec Sp_KillAllProcessInDB '数据库名字'