Solved

Runnung Batch File from Stored Procedure

Posted on 2015-02-05
9
130 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
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 23

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
 

Author Comment

by:tim44202
ID: 40592056
Steve

I tried both methods.  Received this error:
"The system cannot find the path specified."
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 26

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 26

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

17 Experts available now in Live!

Get 1:1 Help Now