-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stored_Procedure_Backup_Without_Parameter.sql
37 lines (32 loc) · 1.55 KB
/
Stored_Procedure_Backup_Without_Parameter.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
USE TSQL2012;
GO
DECLARE @databasename AS NVARCHAR(128);
SET @databasename = (SELECT MIN(name) FROM sys.databases WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb', 'ReportServer', 'ReportServerTempDB'));
WHILE @databasename IS NOT NULL
BEGIN
PRINT @databasename;
SET @databasename = (SELECT MIN(name) FROM sys.databases WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb','ReportServer', 'ReportServerTempDB') AND name > @databasename);
END
GO
SELECT CONVERT(NVARCHAR, GETDATE(), 120)
SELECT REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR, GETDATE(),120), ' ', '_'),':',''), '_', '');
GO
IF OBJECT_ID(N'dbo.BackupDatabase', N'P') IS NOT NULL DROP PROCEDURE dbo.BackupDatabases
GO
CREATE PROCEDURE dbo.BackupDatabase
AS
BEGIN
DECLARE @databasename AS NVARCHAR(128), @timecomponent AS NVARCHAR(50), @sqlcommand AS NVARCHAR(1000);
SET @databasename = (SELECT MIN(name) FROM sys.databases WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb', 'ReportServer', 'ReportServerTempDB'))
WHILE @databasename IS NOT NULL
BEGIN
SET @timecomponent = REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR, GETDATE(),120), ' ', '_'),':',''), '_', '');
SET @sqlcommand = 'BACKUP DATABASE ' + @databasename + 'TO DISK = ''C:\Backup\' + @databasename + '_' + @timecomponent + '.bak''';
PRINT @sqlcommand;
--EXEC(@sqlcommand);
SET @databasename = (SELECT MIN(name) FROM sys.databases WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb','ReportServer', 'ReportServerTempDB') AND name > @databasename);
END;
RETURN;
END;
GO
EXECUTE dbo.BackupDatabase;