Script Backup Todas as bases com Cursor SQL

16 de fevereiro de 2021   |   by administrador
declare @nomeBanco varchar(max)
declare BancosBackup cursor for  SELECT name FROM master.sys.databases where name like '%Pro.%'

OPEN BancosBackup									  		

FETCH NEXT FROM BancosBackup into @nomeBanco			
									
WHILE @@FETCH_STATUS = 0 
BEGIN 

DECLARE @MyFileName varchar(1000)
SELECT @MyFileName = (SELECT 'C:\BKP\'+ @nomeBanco + '_' + convert(varchar(500),GetDate(),112) + '.bak') 
BACKUP DATABASE @nomeBanco TO DISK=@MyFileName				
Select @MyFileName
FETCH NEXT FROM BancosBackup into @nomeBanco					
END							
CLOSE BancosBackup		
DEALLOCATE BancosBackup 
Tags: