views:

1410

answers:

2

How can you insert the date in the filename (a dynamic filename) used in a T-SQL backup script? Using SQL Enterprise Manager to create and schedule a backup job, I'd like to edit the T-SQL created to change the filename of the backed up database to be dbname_date.bak (i.e. northwind_5-1-2009.bak). The next time the backup runs, it will be northwinds_new_date.bak.

+1  A: 

Here's what you really want to know - don't reinvent the wheel. Here's a fantastic script to automate backups that does what you're describing:

http://blog.ola.hallengren.com/

Brent Ozar
Very nice but it doesn't answer my question. I will play around your script though...
Boogie
+1  A: 

basically what you want to do is declare a string variable, set it to the name and then append the date to the end of the variable. then just use the variable where the name of the backup goes

declare @backupname nvarchar(100)
set @backupname = 'northwind_' + getdate() + '.bak'

something like that should work. you might have to sat the getdate() to nvarchar.

DForck42
This is what I was looking for...Thanks. I'll test it out.
Boogie
Had to convert getdate() to MM-DD-YYYY using the CONVERT function and then it worked like a champ!! Thanks.
Boogie
no prob man, glad it works.
DForck42