sql语句还原数据库,多库还原

create table #v_group(tbname varchar(200)) 
insert into #v_group(tbname)values('数据库1') 
insert into #v_group(tbname)values('数据库2') 
insert into #v_group(tbname)values('数据库3') 
declare @v_path varchar(300)--还原路径 
SET @v_path='D:\web\' 
--申明一个游标 
DECLARE MyCursord CURSOR FOR
SELECT *FROM #v_group 
--打开一个游标 
OPEN MyCursord 
--循环一个游标 
DECLARE @tbname varchar(300) FETCH NEXT
FROM MyCursord INTO @tbname 
WHILE @@FETCH_STATUS =0 
BEGIN 
declare @v_backup_database varchar(100),@vname varchar(100),@mdf varchar(100),@ldf varchar(100),@tb1 varchar(300),@tb2 varchar(300) print @tbname 
--开始还原 
CREATE TABLE #TempFileList ( [LogicalName] NVARCHAR(128), [PhysicalName] NVARCHAR(260), [Type] CHAR(1), [FileGroupName] NVARCHAR(128), [Size] NUMERIC(20,0), [MaxSize] NUMERIC(20,0), [FileID] BIGINT, [CreateLSN] NUMERIC(25,0), [DropLSN] NUMERIC(25,0), [UniqueID] UNIQUEIDENTIFIER, [ReadOnlyLSN] NUMERIC(25,0), [ReadWriteLSN] NUMERIC(25,0), [BackupSizeInBytes] BIGINT, [SourceBlockSize] INT, [FileGroupID] INT, [LogGroupGUID] UNIQUEIDENTIFIER, [DifferentialBaseLSN] NUMERIC(25,0), [DifferentialBaseGUID] UNIQUEIDENTIFIER, [IsReadOnly] BIT, [IsPresent] BIT, [TDEThumbprint] VARBINARY(32) )
SELECT @v_backup_database=@v_path+@tbname+'.bak' ,@mdf=@v_path++@tbname+'.mdf',@ldf=@v_path++@tbname+'.ldf' INSERT INTO #TempFileList EXEC('RESTORE FILELISTONLY FROM DISK = '''+@v_backup_database+'''') print @v_backup_databaseSELECT @tb1=LogicalName
FROM #TempFileList
WHERE Type='D'SELECT @tb2=LogicalName
FROM #TempFileList
WHERE Type='L' restore filelistonly
FROM disk=@v_backup_database drop table #TempFileList RESTORE DATABASE @tbname
FROM DISK = @v_backup_database
WITH MOVE @tb1 TO @mdf, MOVE @tb2 TO @ldf 
--结束还原 
FETCH NEXT
FROM MyCursord INTO @tbname
END 
--关闭游标 
CLOSE MyCursord 
--释放资源 
DEALLOCATE MyCursord 
drop table #v_group 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值