?
Solved

Runnung Batch File from Stored Procedure

Posted on 2015-02-05
9
Medium Priority
?
160 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 25

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1500 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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