?
Solved

Runnung Batch File from Stored Procedure

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

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 23

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:tim44202
ID: 40592056
Steve

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

807 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