Solved

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

Posted on 2013-06-11
3
232 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
3 Comments
 
LVL 13

Assisted Solution

by:jonnidip
jonnidip earned 250 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 250 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now