4

I use SQL Server 2008 R2 and want to use Backup Devices for create backup of my database in each day. How can I create dynamic file name in `Backup Devices'.

In below code I Create a Backup Devices but want to have Current Date (YYYY-MM-DD) in my backup file.

EXEC master.dbo.sp_addumpdevice  @devtype = N'disk', @logicalname = N'My Database Backup Location', @physicalname = N'D:\Backup\Backup YYYY-MM-DD.bak'

Thanks in advance

mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128

2 Answers2

6

Try this

DECLARE @fn nvarchar(200)
SET @fn=N'D:\Backup\Backup'+CONVERT(VARCHAR(8), GETDATE(), 112) +'.bak'



EXEC master.dbo.sp_addumpdevice  @devtype = N'disk', 
                @logicalname = N'My Database Backup Location',
                @physicalname = @fn
Igor Borisenko
  • 3,806
  • 3
  • 34
  • 49
0

You can use an intermediate variable, store the file-name appended with the DD-MM-YYYY. and use this intermediate variable in your EXEC statement.

Refer below link for how: Using the result of an expression (e.g. Function call) in a stored procedure parameter list?

Community
  • 1
  • 1
tempidope
  • 823
  • 1
  • 12
  • 29