SQL Backup query with parameters

Hi Guys I want to add parameters to my backup query but I need help to the last part.
This is what I got so far:
DECLARE @HourMinute VARCHAR(8)
DECLARE @HourMinute1 VARCHAR(6)
DECLARE @HourMinute2 VARCHAR(4)
SET @HourMinute = (SELECT CONVERT(VARCHAR(8),GETDATE(),108))
SET @HourMinute1 = REPLACE(@HourMinute, ':', '')
SET @HourMinute2 = LEFT(@HourMinute1,4)
DECLARE @SHORTDATE VARCHAR(11)
DECLARE @SHORTDATE1 VARCHAR(9)
SET @SHORTDATE = (SELECT CONVERT(VARCHAR(11),GETDATE(),105))
SET @SHORTDATE1 = REPLACE(@SHORTDATE, '-', '')
BACKUP DATABASE [FINANCE_Exch] TO  DISK = N'\\backupserver\sqlbackup\CODKSQL03\FINANCE_Exch\Full\CODKSQL03_FINANCE_Exch_Full_' + @SHORTDATE1 + '_' + @HourMinute2 + '.bak' WITH  COPY_ONLY, NOFORMAT, NOINIT,  NAME = N'FINANCE_Exch-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10

Open in new window

Can you help to make this query work.
Thanks
ColoplastAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
What's wrong with your current version?
ColoplastAuthor Commented:
I have changed it to the following:
DECLARE @QUERY VARCHAR(500)
DECLARE @HourMinute VARCHAR(8)
DECLARE @HourMinute1 VARCHAR(6)
DECLARE @HourMinute2 VARCHAR(4)
SET @HourMinute = (SELECT CONVERT(VARCHAR(8),GETDATE(),108))
SET @HourMinute1 = REPLACE(@HourMinute, ':', '')
SET @HourMinute2 = LEFT(@HourMinute1,4)
DECLARE @SHORTDATE VARCHAR(11)
DECLARE @SHORTDATE1 VARCHAR(9)
SET @SHORTDATE = (SELECT CONVERT(VARCHAR(11),GETDATE(),105))
SET @SHORTDATE1 = REPLACE(@SHORTDATE, '-', '')
SET @QUERY = 'BACKUP DATABASE [FINANCE_Exch] TO  DISK = N'\\bucprtv01\sqlbackup\CODKSQL03\FINANCE_Exch\Full\CODKSQL03_FINANCE_Exch_Full_' + @SHORTDATE1 + '_' + @HourMinute2 + '.bak' WITH  COPY_ONLY, NOFORMAT, NOINIT,  NAME = N'FINANCE_Exch-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10'
EXEC(@QUERY)

Open in new window


But I'm getting this error:
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near '\'.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Use:
EXEC sp_ExecuteSQL @Query
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

ColoplastAuthor Commented:
I'm getting the same error
Vitor MontalvãoMSSQL Senior EngineerCommented:
Sorry, I just saw it better now. It miss some single quotes. Please try this version:
DECLARE @QUERY VARCHAR(500)
DECLARE @HourMinute VARCHAR(8)
DECLARE @HourMinute1 VARCHAR(6)
DECLARE @HourMinute2 VARCHAR(4)
SET @HourMinute = (SELECT CONVERT(VARCHAR(8),GETDATE(),108))
SET @HourMinute1 = REPLACE(@HourMinute, ':', '')
SET @HourMinute2 = LEFT(@HourMinute1,4)
DECLARE @SHORTDATE VARCHAR(11)
DECLARE @SHORTDATE1 VARCHAR(9)
SET @SHORTDATE = (SELECT CONVERT(VARCHAR(11),GETDATE(),105))
SET @SHORTDATE1 = REPLACE(@SHORTDATE, '-', '')
SET @QUERY = 'BACKUP DATABASE [FINANCE_Exch] TO  DISK = N''\\bucprtv01\sqlbackup\CODKSQL03\FINANCE_Exch\Full\CODKSQL03_FINANCE_Exch_Full_' + @SHORTDATE1 + '_' + @HourMinute2 + '.bak'' WITH  COPY_ONLY, NOFORMAT, NOINIT,  NAME = N''FINANCE_Exch-Full Database Backup'', SKIP, NOREWIND, NOUNLOAD,  STATS = 10'
EXEC(@QUERY)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ColoplastAuthor Commented:
That did it :-)
Thanks Vitor I knew you could solve it.
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.