数据库数据损坏修复脚本
数据库数据损坏修复脚本
sp_config 'allow updates',1
reconfigure with override
go
update sysdatabases set status=32768
where name ='AdventureWorks '
USE [master]
GO
CREATE DATABASE AdventureWorks ON
( FILENAME = N'C:\AdventureWorks_Data.mdf' )
FOR ATTACH_REBUILD_LOG
GO
--KILL掉某个数据库的所有连接
DECLARE @DBNAME NVARCHAR(100)
DECLARE @SQL NVARCHAR(MAX)
DECLARE @SPID NVARCHAR(100)
SET @DBNAME='TEST11' --★Do
DECLARE CurDBName CURSOR
FOR
SELECT [spid]
FROM sys.sysprocesses WHERE [spid]>=50
AND DBID =DB_ID(@DBNAME)
OPEN CurDBName
FETCH NEXT FROM CurDBName INTO @SPID
WHILE @@FETCH_STATUS = 0
BEGIN
--kill process
SET @SQL = N'kill '+@SPID
EXEC (@SQL)
FETCH NEXT FROM CurDBName INTO @SPID
END
CLOSE CurDBName
DEALLOCATE CurDBName
-------------------------------------------------------------------
-- 1. 将数据库设置为 EMERGENCY 模式
ALTER DATABASE [TEST11] SET EMERGENCY
GO
-- 2. 使用 DBCC CHECKDB 检查损坏情况
DBCC CHECKDB('TEST11')
GO
-- 3. 执行输出中建议的 DBCC 修复语句做数据修复
ALTER DATABASE [TEST11] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
go
--DBCC CHECKDB('TEST11',REPAIR_ALLOW_DATA_LOSS)
--GO
-- 4. 如果成功修复,则将数据库改回正式模式
ALTER DATABASE [TEST11] SET ONLINE
ALTER DATABASE [TEST11] SET MULTI_USER WITH ROLLBACK IMMEDIATE
GO
-- 5. 如果无法,那么在 EMERGENCY 模式下把能够读取的数据弄出来放到一个新建的库里面吧
----------------------------------------------------------------
--海南琼山妇幼保健院修复脚本 sp_config 'allow updates',1 reconfigure with override go #修改数据库状态 update sysdatabases set status=32768 where name ='qsfy ' #重建日志文件 dbcc rebuild_Log('qsfy','D:\DBDATA\qsfy.ldf') #使用 DBCC CHECKDB 检查损坏情况 DBCC CHECKDB('qsfy') GO #执行输出中建议的 DBCC 修复语句做数据修复 ALTER DATABASE [qsfy] SET SINGLE_USER WITH ROLLBACK IMMEDIATE go DBCC CHECKDB('qsfy',REPAIR_ALLOW_DATA_LOSS) #如果成功修复,则将数据库改回正式模式 ALTER DATABASE [qsfy] SET ONLINE ALTER DATABASE [qsfy] SET MULTI_USER WITH ROLLBACK IMMEDIATE GO sp_config 'allow updates',0 reconfigure with override go