Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-06-11
3
Medium Priority
?
288 Views
Last Modified: 2013-06-14
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.
0
Comment
Question by:dejandejanovic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 13

Assisted Solution

by:jonnidip
jonnidip earned 1000 total points
ID: 39237768
Syntax for getdate is: getdate().
Then you need to convert it to a varchar (and choose a format).
For example: select convert(varchar(8), getdate(), 112)
(returns a date in the yyyyMMdd format).

Regards.
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 1000 total points
ID: 39239469
Yup - what jonnidip said.  You need the parens *and* you need to cast it to VARCHAR().

Here are a couple sites that show you conversion options:

http://linesofcode.net/snippets/45
http://networking.ringofsaturn.com/SQL/howtoformatdatetime.php
0
 

Author Comment

by:dejandejanovic
ID: 39248712
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

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question