1CREATE procedure dbo.usp_backup_database
2
3@p_dbname varchar(50),--备份数据库名称
4@p_type varchar(50),--备份类型,fullorlog
5@filename varchar(8000)='' OUTPUT
6
7as
8
9SET NOCOUNT ON
10SET DATEFIRST 1
11
12DECLARE @sql varchar(8000)
13DECLARE @datetime varchar(30)
14DECLARE @path varchar(100)
15
16SET @path = 'E:\backup\sql_data\'
17
18IF @p_type='full'
19BEGIN
20 SET @datetime = CONVERT(varchar(20),getdate(),20)
21 SET @datetime = REPLACE(@datetime,' ','_')
22 SET @datetime = 'DB_Full_'+REPLACE(@datetime,':','-')
23
24 SET @filename = @datetime+'.bak'''
25
26 SET @sql = 'BACKUP DATABASE ' + @p_dbname
27 SET @sql = @sql + ' TO DISK = N'''+@path+@p_dbname + '\'+@filename
28 SET @sql = @sql + ' WITH INIT'
29END
30
31IF @p_type='diff'
32BEGIN
33 SET @datetime = 'DB_Diff_'+CAST(DATEPART ( dw , getDate()) AS char(1))
34 SET @filename = @datetime+'.diff'''
35 SET @sql = 'BACKUP DATABASE ' + @p_dbname
36 SET @sql = @sql + ' TO DISK = N'''+@path+@p_dbname + '\'+@filename
37 SET @sql = @sql + ' WITH INIT,DIFFERENTIAL'
38END
39
40IF @p_type='log'
41BEGIN
42 SET @datetime = 'DB_Log_'+DATENAME ( hh , getdate() )
43 SET @filename = @datetime+'.log'' '
44 SET @sql = 'BACKUP LOG '+ @p_dbname
45 SET @sql = @sql + ' TO DISK = N'''+@path+@p_dbname + '\'+@filename
46 SET @sql = @sql + ' WITH INIT'
47END
48
49
50--SELECT @sql
51
52EXEC (@sql)
53
54SET @filename = REPLACE(@filename,'''','')
55GO
2
3@p_dbname varchar(50),--备份数据库名称
4@p_type varchar(50),--备份类型,fullorlog
5@filename varchar(8000)='' OUTPUT
6
7as
8
9SET NOCOUNT ON
10SET DATEFIRST 1
11
12DECLARE @sql varchar(8000)
13DECLARE @datetime varchar(30)
14DECLARE @path varchar(100)
15
16SET @path = 'E:\backup\sql_data\'
17
18IF @p_type='full'
19BEGIN
20 SET @datetime = CONVERT(varchar(20),getdate(),20)
21 SET @datetime = REPLACE(@datetime,' ','_')
22 SET @datetime = 'DB_Full_'+REPLACE(@datetime,':','-')
23
24 SET @filename = @datetime+'.bak'''
25
26 SET @sql = 'BACKUP DATABASE ' + @p_dbname
27 SET @sql = @sql + ' TO DISK = N'''+@path+@p_dbname + '\'+@filename
28 SET @sql = @sql + ' WITH INIT'
29END
30
31IF @p_type='diff'
32BEGIN
33 SET @datetime = 'DB_Diff_'+CAST(DATEPART ( dw , getDate()) AS char(1))
34 SET @filename = @datetime+'.diff'''
35 SET @sql = 'BACKUP DATABASE ' + @p_dbname
36 SET @sql = @sql + ' TO DISK = N'''+@path+@p_dbname + '\'+@filename
37 SET @sql = @sql + ' WITH INIT,DIFFERENTIAL'
38END
39
40IF @p_type='log'
41BEGIN
42 SET @datetime = 'DB_Log_'+DATENAME ( hh , getdate() )
43 SET @filename = @datetime+'.log'' '
44 SET @sql = 'BACKUP LOG '+ @p_dbname
45 SET @sql = @sql + ' TO DISK = N'''+@path+@p_dbname + '\'+@filename
46 SET @sql = @sql + ' WITH INIT'
47END
48
49
50--SELECT @sql
51
52EXEC (@sql)
53
54SET @filename = REPLACE(@filename,'''','')
55GO