Pages

Saturday, June 1, 2013

How to backup database with datetimestamp in file name?

Execute the following Microsoft SQL Server T-SQL script to demonstrate the backup of AdventureWorks2008 database with the datetimestamp in the backup file name. 
USE master;

DECLARE @FileName NVARCHAR(1024)
SET @FileName ='F:\data\AdventureWorks2008\backup\AW2008'+REPLACE(REPLACE(CONVERT(varchar(100), GETDATE(),100),':','_'),' ','_')+'.BAK'
SET @FileName = REPLACE(@FileName,'__','_')
PRINT @FileName
BACKUP DATABASE AdventureWorks2008 TO  DISK = @FileName WITH NOFORMAT, INIT,  NAME =N'AdventureWorks2008-Full Database Backup'
GO

No comments:

Post a Comment