sql server backup database

Solutions on MaxInterview for sql server backup database by the best coders in the world

showing results for - "sql server backup database"
Jakob
26 Jan 2018
1DECLARE @name VARCHAR(50) -- database name  
2DECLARE @path VARCHAR(256) -- path for backup files  
3DECLARE @fileName VARCHAR(256) -- filename for backup  
4DECLARE @fileDate VARCHAR(20) -- used for file name
5 
6-- specify database backup directory
7SET @path = 'V:\SQL_Backup\01_Diaria\'  
8 
9-- specify filename format
10SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) 
11 
12DECLARE db_cursor CURSOR READ_ONLY FOR  
13SELECT name 
14FROM master.sys.databases 
15WHERE name NOT IN ('master','model','msdb','tempdb')  -- exclude these databases
16AND state = 0 -- database is online
17AND is_in_standby = 0 -- database is not read only for log shipping
18 
19OPEN db_cursor   
20FETCH NEXT FROM db_cursor INTO @name   
21 
22WHILE @@FETCH_STATUS = 0   
23BEGIN   
24   SET @fileName = @path + @name + '_' + @fileDate + '.BAK'  
25   BACKUP DATABASE @name TO DISK = @fileName  
26   WITH INIT, COMPRESSION
27 
28   FETCH NEXT FROM db_cursor INTO @name   
29END   
30
31 
32CLOSE db_cursor   
33DEALLOCATE db_cursor