Saturday, August 20, 2011

Adding current datetime to your DB backup filename

DECLARE @Dbname VARCHAR(20)
DECLARE @Date DATETIME
DECLARE @test VARCHAR(30)

SET @Dbname = 'Test'
SET @Date = GETDATE()
SET @test = 'c:\test' + @Dbname

SELECT @test = @test + CONVERT(VARCHAR(10), @Date, 112) + '.bak'
BACKUP DATABASE @Dbname TO DISK = @test

No comments: