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
01-19
07-28