备份所有数据库代码
2008-12-26 11:48
274 查看
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_backupdb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[p_backupdb] GO /*--备份所有数据库 备份的文件名为数据库名+.bak 将所有的用户数据库(或指定的数据库列表) 备分到指定的目录下. --邹建 2003.10(引用请保留此信息)--*/ /*--调用示例 --备份所有用户数据库 exec p_backupdb @bkpath='c:/',@dbname='' --备份指定数据库 exec p_backupdb @bkpath='c:/',@dbname='客户资料,xzkh_new' --*/ create proc p_backupdb @bkpath nvarchar(260)='', --备份文件的存放目录,不指定则使用SQL默认的备份目录 @dbname nvarchar(4000)='' --要备份的数据库名称列表,不指定则备份所有用户数据库 as declare @sql varchar(8000) --检查参数 if isnull(@bkpath,'')='' begin select @bkpath=rtrim(reverse(filename)) from master..sysfiles where name='master' select @bkpath=substring(@bkpath,charindex('/',@bkpath)+1,4000) ,@bkpath=reverse(substring(@bkpath,charindex('/',@bkpath),4000))+'BACKUP/' end else if right(@bkpath,1)<>'/' set @bkpath=@bkpath+'/' --得到要备份的数据库列表 if isnull(@dbname,'')='' declare tb cursor local for select name from master..sysdatabases where name not in('master','tempdb','model','msdb') else declare tb cursor local for select name from master..sysdatabases where name not in('master','tempdb','model','msdb') and(@dbname like '%,'+name+',%' or @dbname like name+',%' or @dbname like '%,'+name) --备份处理 open tb fetch next from tb into @dbname while @@fetch_status=0 begin set @sql='backup database '+@dbname +' to disk='''+@bkpath+@dbname +'.bak'' with format' exec(@sql) fetch next from tb into @dbname end close tb deallocate tb go
相关文章推荐
- 备份数据库用到的相关代码(在数据库事务中添加)
- window环境下定时备份数据库文件、项目代码并且定期删除
- linux代码、数据库备份
- powershell备份代码和数据库到本地和异地
- sqlserver中打印出某个数据库的所有存储过程代码的例子
- 还原某一个目录下的所有数据库备份文件
- 备份所有数据库
- 规则 2.1:所有数据库代码统一使用小写字母。
- 数据库还原与备份的代码(1)
- Mysql 一次性备份导出/导入恢复所有数据库
- Mysql 一次性备份导出/导入恢复所有数据库
- java数据库备份代码
- CTDB和SAMBA数据库备份代码分析
- 通过T-SQL语句实现数据库备份与还原的代码
- 用mydump对所有数据库进行备份,还原具体案例
- VB备份与还原SQL数据库代码
- 利用代码实现数据库的备份和恢复
- 简单T-Sql备份所有数据库
- php将数据库中所有内容生成静态html文档的代码