Created
July 23, 2025 15:21
-
-
Save pwelter34/3e0f5eaac5fc15f71000e701af4ab5a7 to your computer and use it in GitHub Desktop.
SQL Server Backup All Locale Databases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @name NVARCHAR(256) | |
DECLARE @path NVARCHAR(512) | |
DECLARE @fileName NVARCHAR(512) | |
DECLARE @fileDate NVARCHAR(40) | |
-- specify database backup directory | |
SET @path = 'D:\Backup\Database\' | |
-- specify filename format | |
SELECT @fileDate = CONVERT(NVARCHAR(20),GETDATE(),112) | |
DECLARE db_cursor CURSOR READ_ONLY FOR | |
SELECT [name] | |
FROM master.sys.databases | |
WHERE [name] NOT IN ('master','model','msdb','tempdb') | |
AND [state] = 0 | |
AND is_in_standby = 0 | |
OPEN db_cursor | |
FETCH NEXT FROM db_cursor INTO @name | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
SET @fileName = @path + @name + '_' + @fileDate + '.bak' | |
BACKUP DATABASE @name TO DISK = @fileName WITH COMPRESSION | |
FETCH NEXT FROM db_cursor INTO @name | |
END | |
CLOSE db_cursor | |
DEALLOCATE db_cursor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment