1,使用SQL最简单备份,还原数据库

1 /* 备份 */
2 backup database Test to disk='D:/Test.bak'
3 /* 还原 */
4 restore database Test from disk='D:/Test.bak'

2,为了方便以后的使用,开始对语句进行简单的封装->存储过程

(1)备份

 1 /*******************************************************
2 备份数据库
3 *******************************************************/
4 if exists(select 1 from sys.procedures where name='sp_BackupDB')
5 drop procedure sp_BackupDB
6 go
7 create procedure sp_BackupDB
8 @savePath nvarchar(4000) -- 备份数据库保存位置(目录)
9 ,@dbName nvarchar(4000) -- 需要进行备份的数据库
10 ,@bakName nvarchar(4000) -- 备份文件的名称(不含扩展名)
11 as begin
12 declare @sql nvarchar(4000)
13 /* 验证路径 */
14 if(charindex('/',reverse(@savePath))!=1) begin
15 set @savePath=@savePath+'/'
16 end
17 /* 拼SQL并执行 */
18 set @sql='backup database '+@dbName+' to disk='''+@savePath+@bakName+'.bak'''
19 exec sp_executesql @sql
20
21 /* 返回执行结果(1=成功,0=失败) */
22 if(@@error=0) begin
23 return 1
24 end
25 return 0
26 end

(2)还原

 1 /*******************************************************
2 还原数据库
3 *******************************************************/
4 if exists(select 1 from sys.procedures where name='sp_RestoreDB')
5 drop procedure sp_RestoreDB
6 go
7 create procedure sp_RestoreDB
8 /* 数据库还原后的保存位置(目录)(使用系统默认保存位置:-1) */
9 @savePath nvarchar(4000)
10 ,@backFile nvarchar(4000) -- 需要还原的数据库备份文件
11 ,@defaultName nvarchar(4000) -- 数据库原始名称(备份的原数据库名称)不包含扩展名
12 /* 为数据库重命名(使用数据库默认名称:-1)不包含扩展名
13 如果目录已存在该名称的数据库,将会被覆盖 */
14 ,@dbName nvarchar(4000)
15 as begin
16 declare @newName nvarchar(4000),@sql nvarchar(4000)
17 /* 获取数据库名称 */
18 if(@dbName='-1') begin
19 set @newName=@defaultName
20 end else begin
21 set @newName=@dbName
22 end
23 /* 结束所有对当前数据库的连接 */
24 if exists(select 1 from sys.sysprocesses where dbid=db_id(@defaultName)) begin
25 declare #cs_spid cursor -- 声明游标
26 for
27 select #cs_spid=convert(varchar,spid) from sys.sysprocesses where dbid=db_id(@defaultName)
28 open #cs_spid
29 declare @spid varchar(20)
30 fetch next from #cs_spid into @spid -- 赋值并前进到下一条
31 while(@@fetch_status=0) begin -- 在fetch失败前执行
32 exec ('kill '+@spid) -- 结束对操作库的连接(exec执行SQL语句1)
33 fetch next from #cs_spid into @spid
34 end
35 close #cs_spid
36 deallocate #cs_spid -- 释放游标
37 end
38 /* 创建执行语句 */
39 set @sql='restore database '+@newName+' from disk='''+@backFile+''' with replace'
40 if(@savePath!='-1') begin
41 -- 验证路径
42 if(charindex('/',reverse(@savePath))!=1) begin
43 set @savePath=@savePath+'/'
44 end
45 set @sql=@sql+', move '''+@defaultName+''' to '''+@savePath+@newName+'.mdf'''
46 set @sql=@sql+', move '''+@defaultName+'_log'' to '''+@savePath+@newName+'_log.ldf'''
47 end
48 /* 执行操作 */
49 exec sp_executesql @sql -- (exec执行SQL语句2)
50 /* 返回执行结果(1=成功,0=失败) */
51 if(@@error=0) begin
52 return 1
53 end
54 return 0
55 end

SQL语句备份和还原数据库的更多相关文章

  1. SQL语句备份和还原数据库(转)

    1,使用SQL最简单备份,还原数据库 1 /* 备份 */ 2 backup database Test to disk='D:/Test.bak' 3 /* 还原 */ 4 restore data ...

  2. SQL Server 备份和还原数据库

    备份: --完整备份 ) set @db_name = 'WSS_Content_Test'; ) set @db_location = 'D:\spbr0002\0000000B.bak'; --保 ...

  3. 怎么用SQL语句备份和恢复数据库?

    BACKUP DATABASE "mydb" TO DISK ='C:\mybak.db' with init RESTORE DATABASE "mydb" ...

  4. sql语句备份/导入 mysql数据库或表命令

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq1355541448/article/details/30049851

  5. MSSQL - 备份和还原数据库

    SQL语句备份和还原数据库:http://blog.csdn.net/liuhelong/article/details/3335687 1.MSSQL - SqlServer:此数据库处于单用户模式 ...

  6. ASP.NET中使用代码来进行备份和还原数据库

    ASP.NET中使用代码来进行备份和还原数据库  SQL代码: 1 2 3 4 5 -- 备份数据库 backup database db_CSManage to disk='c:\backup.ba ...

  7. SQL Server 备份和还原

    SQL Server 备份和还原   SQL Server 备份 恢复模式 SQL Server 数据恢复模式分为三种:完整恢复模式.大容量日志恢复模式.简单恢复模式. 完整恢复模式 默认的恢复模式, ...

  8. 在ASP.NET中备份和还原数据库

        昨天看了<C#项目实录>中的进销存管理系统,和其他书里讲的案例一样,无非也就是数据库增删查改,但是这个进销存系统中有一个备份和还原数据库的功能,蛮有兴趣的,看了一下代码,原来如此, ...

  9. mysql备份、还原数据库(命令行)

    这里记录下MySQL如何通过命令行备份和还原数据库. 简单的三个步骤 方法很简单,可以分为三个步骤: 1.打开cmd控制台(命令行). 2.输入相应命令完成备份还原操作. 3.关闭cmd控制台. 就和 ...

随机推荐

  1. 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同

    https://blog.csdn.net/qq_22522663/article/details/78729029 1. tf.Variable与tf.get_variabletensorflow提 ...

  2. NumPy 广播(Broadcast)

    NumPy 广播(Broadcast) 广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 如果两个数组 a 和 b ...

  3. random 模块 时间模块(time) sys模块 os模块

    random  模块 1.随机小数 random.random()  0-1内的随机小数 random.uniform(1,5)  1-5范围内的随机小数 2.随机整数 random.randint( ...

  4. stm32 开发中startup.s文件中常见的命令功能

    由于C的普及以及编译器的发展,越来越多的软件工程师在编程时很少有机会接触到汇编语言.在ARM的开发中,我们不可避免的会遇到启动文件的编写,在KEIL环境中一般采用了startup.s的文件作为启动代码 ...

  5. vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法

    原文:http://www.jb51.net/article/129270.htm main.js入口文件配合vue-router写这个 router.afterEach((to,from,next) ...

  6. Python: print stdout同行输出

    项目中发现,PyCharm运行youtube_dl下载,其进度在同行显示,即替换上行输出. 稍做研究,记录下来: #coding=utf-8 from __future__ import print_ ...

  7. java_15 System类

    1.System类 2.System类方法 (1)currentTimeMillis() public static void main(String[] args) { long start = S ...

  8. 线特征---Edge Drawing(七)

    http://ceng.anadolu.edu.tr/cv/edgedrawing/ References C. Topal, C. Akinlar, Edge Drawing: A Combined ...

  9. devexpress之barManager 使用

    这次我不想使用ribboncontrol 控件 作为窗口菜单栏,也不想用传统的那种字体的方式 标题栏 一.Bars 1.   把BarManager组件添加到窗体中后,会自动创建两个空的 bars: ...

  10. Bonding

    一.简介 双网卡配置设置虚拟为一个网卡实现网卡的冗余,其中一个网卡坏掉后网络通信仍可正常使用,实现网卡层面的负载均衡和高可用性   二.原理 网卡工作在混杂(promisc)模式,接收到达网卡的所有数 ...