Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Executing a .bat file from SQL

Author  Topic 

bevans
Starting Member

10 Posts

Posted - 2012-04-12 : 10:30:03
Hi,
I currently have the following setup:
I'm executing the following SQL command: Which executes a scheduled task on an app server
exec master.dbo.xp_cmdshell 'schtasks /run /s Appserver-test /U gray\svc_serviceaccount /P 123456 /tn "\EXPORT TEST"'

This scheduled task runs this file:

C:\Export parameters\exporttest.bat

I was wondering, is it possible to execute this bat file directly, via a sql script. So instead of executing the scheduled task, which executes the bat file, I simply execute the bat file directly instead

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-12 : 18:01:49
you can use xp_cmdshell itself to execute it directly

http://blog.sqlauthority.com/2007/06/27/sql-server-running-batch-file-using-t-sql-xp_cmdshell-bat-file/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-12 : 18:15:36
exec master.dbo.xp_cmdshell 'C:\Export parameters\exporttest.bat'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-04-13 : 01:51:52
I have a batch file which takes database and server names as input from the user using cmdprompt(sqlcmd) and produces a backup of the db.
I tried running the file using Sql Server(using xp_cmd shell). But, instead of running the batch file, the statement produced a table with a single column(named OUTPUT) and these were the contents of the column:

NULL
C:\WINDOWS\system32>echo off
Enter database name: Enter_Your_SQL_ServerName_here:
Sqlcmd: '-S': Missing argument. Enter '-?' for help.
NULL
NULL

This is actually the code and maybe the compiled view of the code.
So I googled this for a while and read somewhere that I could run the batch file as follows:

1. Create a package in SSIS containing the batch file in the form of an "Execute Process Task". This link throws some light on that:

[url]http://msdn.microsoft.com/en-us/library/ms141166.aspx[/url]

2. Save the package and then call the package as part of a job step.

3. Run the Job.

This worked for me. Hope it works for you as well.

Vinu Vijayan

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page
   

- Advertisement -