DECLARE @db nvarchar(50) set @db='my.datab.name' --close all connections DECLARE @SPId int DECLARE @CMD varchar(100) DECLARE my_cursor CURSOR FAST_FORWARD FOR SELECT SPId FROM MASTER..SysProcesses WHERE DBId = DB_ID(@db) AND SPId <> @@SPId OPEN my_cursor FETCH NEXT FROM my_cursor INTO @SPId WHILE @@FETCH_STATUS = 0 BEGIN SELECT @CMD = 'KILL ' + CAST(@SPId AS varchar(10)) EXEC (@CMD) FETCH NEXT FROM my_cursor INTO @SPId END CLOSE my_cursor DEALLOCATE my_cursor exec('ALTER DATABASE ['+ @db +'] SET SINGLE_USER WITH NO_WAIT') dbcc checkdb(@db, REPAIR_ALLOW_DATA_LOSS) exec('ALTER DATABASE ['+ @db +'] SET MULTI_USER WITH NO_WAIT')
try another dbcc checkdb afterwards to be sure it’s fixed