Solved

How do I execute a Windows Batch File from SQL?

Posted on 2010-08-25
7
262 Views
Last Modified: 2012-05-10
I have a job set up that in step 1 Deletes the data from a specified table.
In step 2 I would like to have the program run a Bat file I set up. This Bat file is the BCP import.

The bat file works fine if I run it on a windows scheduler, but I figured there has to be a way to run in from with in a SQL job.

I am using SQL Manager 2010, and in there one of the command types is Operating-system command or executable program
Under this option is a box to put in the command.
But I'm unsure of the command I need to use to make this work.

Any help is appreciated.

Thanks

Chip
0
Comment
Question by:OSXFreak
7 Comments
 
LVL 11

Expert Comment

by:Coast-IT
ID: 33521638
0
 
LVL 5

Accepted Solution

by:
ByteSleuth earned 250 total points
ID: 33521652
0
 
LVL 4

Expert Comment

by:javaftper
ID: 33521667
Here are two ways of running batch file using SQL, one without parameters and one with-
1) Running standalone batch file (without passed parameters)
EXEC master..xp_CMDShell 'c:yourfile.bat'

2) Running parameterised batch file
DECLARE @PassedVariable VARCHAR(100)
DECLARE @CMDSQL VARCHAR(1000)
SET @PassedVariable = '127a'
SET @CMDSQL = 'c:yourfile.bat' + @PassedVariable
EXEC master..xp_CMDShell @CMDSQL
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:OSXFreak
ID: 33521996
I think I'm just looking to do the xp_CMDShell as you all suggest.. BUT my question is how do I know if I hae that installed?
I tried to type in EXEC master..xp_CMDShell 'c:qoh.bat' and run it, but it gave me a failure. So next question. what does the "master" mean in this, is this the DB table? Do I need to change this?

Anyhow As I said it failed, so not sure where I went wrong..
I've attached the screen shot of what I'm looking at.. I know it's something simple..

Oh and I did move the QOH.bat to the C drive :)

Thanks,

Chip
sql.JPG
0
 
LVL 4

Expert Comment

by:javaftper
ID: 33522072
as you are using sequel server 2010 this sounds like a permissions issue.   xp_CMDShell is an embedded store procedure from Microsoft so it will be included in your installation.  You will just need to grant your user access to the procedure.

Follow the steps in this article if you are unsure on how to grant access to a stored procedure-

http://www.mssqltips.com/tip.asp?tip=1020
0
 

Author Comment

by:OSXFreak
ID: 33522393
ok did the enable part, and when I look at the event manager I see this

Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.

I tried to run after doing the enable and failure which led me to event log. Any ideas on the Reconfigure statement?

Chip
0
 
LVL 4

Assisted Solution

by:javaftper
javaftper earned 250 total points
ID: 33522596
run the following two statements in order-

1.
sp_configure 'xp_cmdshell', '0'

2.
sp_configure 'xp_cmdshell', '1'
RECONFIGURE WITH OVERRIDE
GO

For more info on RECONFIGURE statement see here-
http://technet.microsoft.com/en-us/library/ms176069.aspx
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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