Solved

How do I execute a Windows Batch File from SQL?

Posted on 2010-08-25
7
259 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting the 3 middle digits 4 35
Testing connection to sql 7 57
SQL Agent Timeout 5 47
CREATE DATABASE ENCRYPTION KEY 1 56
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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