利用存储过程进行网络备份 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

一、创建存储过程

create procedure bkdatabase

(@dbname varchar(15))      传回数据库名

as

DECLARE @strPath NVARCHAR(200)

set @strPath = convert(NVARCHAR(19),getdate(),120)

set @strPath = REPLACE(@strPath, ':', '')

set @strPath = REPLACE(@strPath, '-' , '_')

set @strPath = REPLACE(@strPath, ' ' , '_')

set @strPath = '\\192.168.1.1\bak\' + @dbname + '_backup_'+ @strPath + '.bak'

BACKUP DATABASE @dbname TO DISK =@strPath WITH NOINIT , NOUNLOAD , NOSKIP , STATS = 10, NOFORMAT

(注:bak文件夹必须共享,设置为everyone都有写入权限。convert(NVARCHAR(19),getdate(),120)中也可以使用112,后面的几个set也就可以省略。)

 
二、新增作业,建立排程

首先要开启 SQL Agent 服务,然后选择“作业”,右击“新增作业”,建立作业名称。
1.  选择“步骤”,在下面选择“新增” , 在步骤名称中输入名称;在命令中输入 EXEC BKDATABASE TEST

EXEC BKDATABASE VLTEST 点击分析命令,幷提示命令分析成功,最后点击确定。

 testvltest为传入的参数,也就是需要备份的数据库名

选择“排程”,在下面选择“新增”,输入名称,按照要求设定排程的时间。

 
1.  设定好后就可以按照要求进行自动备份了。