T-SQL 备份和还原
2009-09-14 00:00:00 来源:WEB开发网Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:恢复到特定的备份日期
*/
IF DB_ID('db') IS NOT NULL
DROP DATABASE db;
GO
CREATE DATABASE db;
GO
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1));
GO
BACKUP DATABASE db TO DISK='c:\1.bak' WITH FORMAT;
GO
INSERT INTO db.dbo.T DEFAULT VALUES;
GO
WAITFOR DELAY '00:00:01';
DECLARE @Datetime BINARY(128);
SET @Datetime=CAST(GETDATE() AS BINARY(128));
SET CONTEXT_INFO @Datetime
GO
INSERT INTO db.dbo.T DEFAULT VALUES;
GO
BACKUP LOG db TO DISK='c:\2.bak' WITH FORMAT;
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH NORECOVERY,REPLACE;
GO
DECLARE @Now DATETIME;
SET @Now=DATEADD(SECOND,-1,(SELECT CAST(CONTEXT_INFO() AS DATETIME)));
--SET @Now=DATEADD(MS,-20,(SELECT CAST(CONTEXT_INFO() AS DATETIME)));
RESTORE LOG db FROM DISK='c:\2.bak' WITH RECOVERY,STOPAT=@Now;
GO
SELECT * FROM db.dbo.T
DROP DATABASE db;
GO
Code
/*
Author:Terry.Sai.M.J 浪客
Location:BeiJing
DateTime:GETDATE()
Description:使用日志恢复数据库
*/
IF DB_ID('db') IS NOT NULL
DROP DATABASE db;
GO
CREATE DATABASE db;
GO
CREATE TABLE db.dbo.T(ID INT PRIMARY KEY IDENTITY(1,1));
GO
BACKUP DATABASE db TO DISK='c:\1.bak' WITH FORMAT;
GO
INSERT INTO db.dbo.T DEFAULT VALUES;
GO
BACKUP LOG db TO DISK='c:\2.bak' WITH FORMAT;
GO
INSERT INTO db.dbo.T DEFAULT VALUES;
GO
BACKUP LOG db TO DISK='c:\3.bak' WITH FORMAT;
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH RECOVERY,REPLACE;
GO
SELECT * FROM db.dbo.T
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH NORECOVERY,REPLACE;
GO
RESTORE LOG db FROM DISK='c:\2.bak' WITH RECOVERY;
GO
SELECT * FROM db.dbo.T
GO
RESTORE DATABASE db FROM DISK='c:\1.bak' WITH NORECOVERY,REPLACE;
GO
RESTORE LOG db FROM DISK='c:\2.bak' WITH NORECOVERY;
GO
RESTORE LOG db FROM DISK='c:\3.bak' WITH RECOVERY;
GO
SELECT * FROM db.dbo.T
GO
DROP DATABASE db;
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
- ››SQL SERVER无法安装成功,sqlstp.log文件提示[未发...
- ››Sql Server中通过父记录查找出所有关联的子记录
- ››SqlServer触发器、存储过程和函数
- ››SQL Server 中的事务(含义,属性,管理)
- ››Sqlite数据库插入和读取图片数据
- ››Sql server 2005拒绝了对对象 'xx表' (数...
- ››Sql server 2005拒绝了对对象 'xx表' (数...
更多精彩
赞助商链接