Solved

How do I execute a Windows Batch File from SQL?

Posted on 2010-08-25
7
267 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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
T-SQL to Update Table Dynamically 2 55
Set the max value for a column 7 39
How to simplify my SQL statement? 14 55
Query to Add Late Tolerance 10 76
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

791 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