Link to home
Start Free TrialLog in
Avatar of dejandejanovic
dejandejanovic

asked on

Sql backup script show "Incorect sintax near '+' -". Need someone to check code.

Hello,
I'm trying to create daily backup of my local databases. Currently I'm testing below code, but of course not sucessfull.
Well, code is working, but I would like to have daily different file names. I have in mind for bak files to include also a date. So, that each daily file would have name associated to day of backup.
When I have execute below code Sql server show me error: Incorrect sintax near '+'.

USE Test;
GO
BACKUP DATABASE Test1
TO DISK = 'D:\Backups\Test_' + getdate + '.Bak'
   WITH FORMAT,
      MEDIANAME = 'Z_SQLServerBackups',
      NAME = 'Full Backup of Test;
GO

Open in new window


Thanks in advance for help.
SOLUTION
Avatar of jonnidip
jonnidip
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dejandejanovic
dejandejanovic

ASKER

Thank you for tips, and working result is here:
USE Test;
GO
DECLARE @DateBackup varchar(50)

SELECT @DateBackup = (SELECT 'D:\Test_' + convert(varchar(50),GetDate(),112) + '.bak') 
BACKUP DATABASE Test
TO DISK = @DateBackup 
   WITH FORMAT,
      MEDIANAME = 'Z_SQLServerBackups',
      NAME = 'Full Backup of Test';
GO

Open in new window