批量还原数据库 SQL Server 2008
1.如果你够懒,不想一步一步点路径,一步一步选择
2.如果你连单个备份数据库的存储过程都不想多执行,一般每还原一个需要修改数据库名
下面的脚本适合你:
/**********************************************
Description:This script help you restore database batch.
Pay attention:You'd better name the database like databaseName+number.
The database name is nessasery. This script not perfect,and please make it progress if you want. Author:jiangxiaoqaing
Script Date:2013-09-29
Modify history: **********************************************/ ALTER procedure [dbo].[sp_restoreDBBatch]
--The path your .bak file store
@database_bak_path varchar(200),
--The distination you want your database file store
@database_path varchar(200)
as
begin
declare @bak_databaseName varchar(200),
@DynamicSql varchar(500)=null /*Judge the '#tmpTable' object if exists,the #tmpTable store backup
database name and path*/
if (OBJECT_ID('#tmpTable') is not null)
drop table #tmpTable
create table #tmpTable
(
DBName varchar(200)
) /*using extended procedure xp_cmdshell to get the path and name*/
set @DynamicSql='cd /d "'+@database_bak_path+'"&&dir /a /b /s *.bak'
insert into #tmpTable exec xp_cmdshell @DynamicSql /*If the backup path not exists,make the directory*/
set @DynamicSql='if not exist '+@database_path+' mkdir '+@database_path+''
exec xp_cmdshell @DynamicSql declare bak_DBPathandName cursor
for
select DBName from #tmpTable
open bak_DBPathandName
while @@FETCH_STATUS=0
begin
fetch next from bak_DBPathandName into @bak_databaseName
declare @s varchar(200)
/*Get the database backup file name,store your specify path*/
set @s= reverse(substring(reverse(@bak_databaseName),0,CHARINDEX('\',reverse(@bak_databaseName))))
/*To get the database name,and tick the number*/
BEGIN
WHILE PATINDEX('%[0-9]%',@S) > 0
BEGIN
set @s=stuff(@s,patindex('%[0-9]%',@s),1,'')
end
/*Tick the '.bak' affix*/
set @s=replace(@s,'.bak','')
end
/*Backup single database*/
exec restore_database_proc @bak_databaseName,@s,@database_path
end
close reachDBName
deallocate reachDBName
end
Resore single DB:
/*******************************************
Descript:SQL server 2008 backup database.
Author:jiangxiaoqiang
Date:2013-09-26
Modify history: *******************************************/ ALTER PROCEDURE [dbo].[restore_database_proc]
@database_bak_path varchar(100),--bak file store path
@database_name varchar(100),--The new database name(Not a LogicalName and PhysicalName)
@database_path varchar(200)--restore new database file store path,the path you want to store data file
AS
--exec( 'RESTORE FILELISTONLY FROM DISK = N''' + @database_bak_path + '''')
--select * from tempdb..sysobjects where name ='#tmp_file'
if OBJECT_ID('tempdb..#tmp_file') is not null
DROP TABLE #tmp_file create table #tmp_file
(
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) NULL,
UniqueID uniqueidentifier,
ReadOnlyLSN numeric(25,0) NULL,
ReadWriteLSN numeric(25,0) NULL,
BackupSizeInBytes bigint,
SourceBlockSize int,
FileGroupID int,
LogGroupGUID uniqueidentifier NULL,
DifferentialBaseLSN numeric(25,0) NULL,
DifferentialBaseGUID uniqueidentifier,
IsReadOnly bit,
IsPresent bit,
TDEThumbprint varbinary(32)
)
--Database datafile full path
declare @database_mdf_path varchar(1000)
--Database log file full path
declare @database_log_path varchar(1000) --The old database name
declare @database_mdf_oldname varchar(1000)
--The old database old log name
declare @database_log_oldname varchar(1000) set @database_mdf_path = @database_path + '/' + @database_name + '.mdf'
set @database_log_path = @database_path + '/' + @database_name + '_Log.ldf' --INSERT INTO #tmp_file EXEC ('restore_database_proc N''' + @database_bak_path + '''')
INSERT INTO #tmp_file EXEC ('RESTORE FILELISTONLY FROM DISK = N''' + @database_bak_path + '''')
set @database_mdf_oldname = (select LogicalName from #tmp_file where Type = 'D')
set @database_log_oldname = (select LogicalName from #tmp_file where Type = 'L')
--select @database_mdf_oldname=LogicalName from #tmp_file where Type = 'D' --select @database_log_oldname =LogicalName from #tmp_file where Type = 'L'
exec(
'
RESTORE DATABASE ' + @database_name +
'
FROM DISK = ''' + @database_bak_path + '''' +
'
WITH
MOVE ''' + @database_mdf_oldname + ''' TO ''' + @database_mdf_path + ''',' +
'
MOVE ''' + @database_log_oldname + ''' TO ''' + @database_log_path + ''''
)
批量还原数据库 SQL Server 2008的更多相关文章
- 备份数据库SQL Server 2008下实测
下面的存储过程适用: 1.一次想备份多个数据库. 2.只需要一步操作,在有存储过程的条件下. 3.可以根据自己的需要修改存储过程. /*----------------------------- De ...
- 九、数据库——sql server 2008导入excel
昨天分配给我一个活,让我手动录入新闻网页的数据,包括每条新闻的标题.时间和链接. 一开始,就是按照最原始的手动录入的方法,一条条的录入.发现这简直就是在浪费时间,于是就想了一种新方法. 1.将网页中的 ...
- excel批量导入数据库SQL server
思路: 第一是文件上传,可以参照Jakarta的FileUpload组件,用普通的Post也就行了.第二是Excel解析,用JSL或者POI都行第三是数据保存,这个应该简单吧,一个循环,一行对应一条数 ...
- 从远程服务器数据库中同步数据到本地数据库 sql server 2008 开启分布
控制面板\所有控制面板项\管理工具 打开“管理工具――组件服务”,以此打开“组件服务――计算机”,在“我的电脑”上点击右键.在MSDTC选项卡中,点击“安全配置”按钮. 在安全配置窗口中做如下设置: ...
- SQL Server 2008 R2 数据库安装
操作系统 Windows server 2008 R2 数据库 SQL Server 2008 R2 注意:SQL Server 2008 R2需要操作系统首先安装.NET Frame ...
- .Net EF Core数据库使用SQL server 2008 R2分页报错How to avoid the “Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement.”
一. 问题说明 最近.Net EF core 程序部署到服务器,服务器数据库安装的是SQL server 2008 R2,我本地用的的是SQL server 2014,在用到分页查询时报错如下: H ...
- Vcenter虚拟化三部曲----SQL Server 2008 R2 数据库安装
操作系统 Windows server 2008 R2 数据库 SQL Server 2008 R2 注意:SQL Server 2008 R2需要操作系统首先安装.NET Frame ...
- .NET Core EF框架使用SQL server 2008数据库分页问题:Incorrect syntax near 'OFFSET'. Invalid usage of the option NEXT in the FETCH statement
一. 问题 最近.Net Core程序部署到服务器,采用EF6.本地数据库是SQL server 2016,服务器数据库安装的是SQL server 2008 R2,在用到分页查询时报错如下: { & ...
- sql server ------创建本地数据库 SQL Server 排序规则
sql server完整复制数据库 sql server导入导出方法 SQL Server 排序规则
随机推荐
- CF#FF(255)-div1-C【水题,枚举】
[吐槽]:本来没打算写这题的题解的,但惨不忍睹得WA了13次,想想还是记录一下吧.自己的“分类讨论能力”本来就很差. 刚开始第一眼扫过去以为是LIS,然后忽略了复杂度,果断TLE了,说起来也好惭愧,也 ...
- box-sizing的学习和认识
先上自己写的DEMO代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- HDU 4752 Polygon(抛物线长度积分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4752 题意:给出一个抛物线和一个简单多边形.求抛物线在多边形内部的长度. 思路:首先求出多边形所有边和 ...
- 重新安装sqlyog时备份数据库链接列表的方法
一般在本机C:\Documents and Settings\Administrator\Application Data\SQLyog这个目录下有个sqlyog.ini文件.把这个文件备份一下就ok ...
- MyEclipse 10离线安装PyDev插件
PyDev for Eclipse, 经过测试,一般在线安装会失败(不能访问某些网站所致) 以下为离线安装步骤 1 下载 PyDev 2.8.2, 链接:http://sourceforge.net ...
- [POJ2777]Count Color(线段树)
题目链接:http://poj.org/problem?id=2777 给你一个长为L想线段,向上面染色,颜色不超过30种,一共有O次操作,操作有两种: C a b c 在[a,b]上染上c颜色 P ...
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- 【笨嘴拙舌WINDOWS】剪切板
Windows剪贴板是一种比较简单同时也是开销比较小的IPC(InterProcess Communication,进程间通讯)机制.Windows系统支持剪贴板IPC的基本机制是由系统预留的一块全局 ...
- 【笨嘴拙舌WINDOWS】GDI映射方式
TextOut(hdc,100,100,TEXT(“Love China”),10) 这句GDI函数的作用是在坐标点(100,100)的位置输出一个“Love China”字符串: GDI函数作为硬件 ...
- web.xml中webAppRootKey
------------------------------------------------------------------------------------------------1. w ...