日期:2014-05-17 浏览次数:20484 次
先停止sql服务 exec sp_detach_db @dbname='book' --重建日志 exec sp_attach_single_file_db @dbname='book', @physname='D:\Program Files\Microsoft SQL Server\MSSQL\Data\book_Data.MDF'
------解决方案--------------------
给个通过日志恢复列子:
USE master --创建数据库 CREATE DATABASE db ON PRIMARY( NAME='db_data', FILENAME= 'c:\db_data.mdf') LOG ON( NAME='db_log', FILENAME ='c:\db.ldf') GO --创建表 CREATE TABLE db.dbo.ta(id int) INSERT db.dbo.ta SELECT id FROM sysobjects GO --做文件组备份 BACKUP DATABASE db TO DISK='c:\db.bak' WITH FORMAT --备份后,再做数据处理 CREATE TABLE db.dbo.tb(id int) INSERT db.dbo.tb SELECT id FROM sysobjects GO /*--下面演示了破坏数据文件的处理,这些操作在操作系统中进行 1. 停止SQL Server服务(msqlserver服务) 2. 删除文件 c:\db_data.ndf (模拟破坏) 3. 重新SQL Server服务,此时数据库DB置疑 --*/ GO --下面演示了如何恢复数据 --首先要备份当前日志 BACKUP LOG db TO DISK='c:\db_log.bak' WITH FORMAT,NO_TRUNCATE --利用文件组备份恢复破坏的文件 RESTORE DATABASE db FROM DISK='c:\db.bak' WITH NORECOVERY --还原到日志点 RESTORE LOG db FROM DISK='c:\db_log.bak' WITH RECOVERY --显示已经恢复的数据 SELECT COUNT(*) FROM db.dbo.ta SELECT COUNT(*) FROM db.dbo.tb GO --删除测试 DROP DATABASE db
------解决方案--------------------
用dbcc checkdb检查一下数据库中是否存在分配错误和一致性错误。
如果存在,先修复这些错误。
dbcc checkdb ('数据库名', REPAIR_ALLOW_DATA_LOSS)
------解决方案--------------------
清除日志: DECLARE @LogicalFileName sysname, @MaxMinutes INT, @NewSize INT USE szwzcheck -- 要操作的数据库名 SELECT @LogicalFileName = 'szwzcheck_Log', -- 日志文件名 @MaxMinutes = 10, -- Limit on time allowed to wrap log. @NewSize = 20 -- 你想设定的日志文件的大小(M) -- Setup / initialize DECLARE @OriginalSize int SELECT @OriginalSize = size FROM sysfiles WHERE name = @LogicalFileName SELECT 'Original Size of ' + db_name() + ' LOG is ' + CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' + CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB' FROM sysfiles WHERE name = @LogicalFileName CREATE TABLE DummyTrans (DummyColumn char (8000) not null) DECLARE @Counter INT, @StartTime DATETIME, @TruncLog VARCHAR(255) SELECT @StartTime = GETDATE(), @TruncLog = 'BACKUP LOG ' + db_name() + ' WITH TRUNCATE_ONLY' DBCC SHRINKFILE (@LogicalFileName, @NewSize) EXEC (@TruncLog) -- Wrap the log if necessary. WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName) AND (@OriginalSize * 8 /1024) > @NewSize BEGIN -- Outer loop. SELECT @Counter = 0 WHILE ((@Counter < @OriginalSize / 16) AND (@Counter < 50000)) BEGIN -- update INSERT DummyTrans VALUES ('Fill Log') DELETE DummyTrans SELECT @Counter = @Counter + 1 END EXEC (@TruncLog) END SELECT 'Final Size of ' + db_name() + ' LOG is ' + CONVERT(VARCHAR(30),size) + ' 8K pages or ' + CONVERT(VARCHAR(30),(size*8/1024)) + 'MB' FROM sysfiles WHERE name = @LogicalFileName DROP TABLE DummyTrans SET NOCOUNT OFF 把szwzcheck换成你数