Solved

Runnung Batch File from Stored Procedure

Posted on 2015-02-05
9
154 Views
Last Modified: 2015-02-06
I am trying to execute a batch file from within a stored procedure on SQL Server 2005

Here is the command I am using

EXEC master.dbo.xp_cmdshell 'C:\Program Files\Advanced Query Tool v10\jHaven_batch_sample.bat'

Here is the error message I receive

ERROR MESSAGE
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

I also tried

EXEC master.dbo.xp_cmdshell "'C:\Program Files\Advanced Query Tool v10\jHaven_batch_sample.bat'"
ERROR
The filename, directory name, or volume label syntax is incorrect.

EXEC master.dbo.xp_cmdshell "C:\Program Files\Advanced Query Tool v10\jHaven_batch_sample.bat"
ERROR MESSAGE
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

EXEC master.dbo.xp_cmdshell '"C:\Program Files\Advanced Query Tool v10\jHaven_batch_sample.bat"'
The filename, directory name, or volume label syntax is incorrect.

What am I doing wrong?
0
Comment
Question by:tim44202
[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
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 24

Expert Comment

by:NVIT
ID: 40591948
Have you tried single quotes ' instead of double ""?

EXEC master.dbo.xp_cmdshell 'C:\Program Files\Advanced Query Tool v10\jHaven_batch_sample.bat'
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 40591952
It doesn't like the spaces in the directory name

Either of these will work (tested both, both appear to work):

EXEC master.dbo.xp_cmdshell 'C:\"Program Files"\"Advanced Query Tool v10"\jHaven_batch_sample.bat'

Open in new window

Or replace the directory names with the old 8.3 format (dir /x from a CMD prompt to see them):

EXEC master.dbo.xp_cmdshell 'C:\Progra~1\Advanc~1\jHaven_batch_sample.bat'

Open in new window

0
 

Author Comment

by:tim44202
ID: 40591954
Yes that is the first example in the body of the question
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:tim44202
ID: 40592056
Steve

I tried both methods.  Received this error:
"The system cannot find the path specified."
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 40592062
Hmmm, I tested both locally and they worked.

Does the service account that SQL Server is running under have permissions to read from the path specified ?
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 40592123
You need to make sure that batch file is on the sql server.
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 40593672
Use this:
EXEC master.dbo.xp_cmdshell '"C:\Program Files\Advanced Query Tool v10\jHaven_batch_sample.bat"'

Open in new window

In SQL the single quotes delimit a string but inside you have to wrap the command in double quotes because of the spaces in the folder names.
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 40593680
More than that I encourage you to actually grab the result output of that command like this:
create table #output (id int identity, result_line varchar(1000))
insert into #output (result_line)
EXEC master.dbo.xp_cmdshell '"C:\Program Files\Advanced Query Tool v10\jHaven_batch_sample.bat"'
select * from #output where result_line is not null

Open in new window

This way you can check the result inside SQL and control what happens next.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40594590
I was wondering, why the grade B was given
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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