Calling a batch file from SQL Server


  • Hi,

    I need to call a batch file from sql server. The batch files exists in a different machine and it has to execute in that machine. I know batch files can be executed using xp_cmdshell in sql server, the machine where the batch file has to execute there is no sql server installed. Will I still be able to call the batch file and make it run in that machine??



    Friday, May 28, 2010 3:50 PM


All replies

  • Here is a link that describes 3 ways to do this using: PSEXEC (from SysInternals), Windows Scripting Host, and Powershell.

    Hope one of them is suitable for you.


    Friday, May 28, 2010 5:51 PM
  • In addition and if you have to use SQL Server, then can use SQL Server Agent job with a "Operating system" step.
    Friday, May 28, 2010 6:25 PM
  • if you are to run sql server job to execute batch file, make sure sql serevr agent job has permission on remote drive and it is maped
    Monday, May 31, 2010 7:24 AM
  • When you execute a batch file from a network share, you're executing it on the local machine, not the remote machine.

    If you want to execute a program on another server, you can use a stored procedure on that server to invoke the command, and call that stored procedure from the local mcahine.

     You could also create a web service on the remote server that invoked the command you want to execute.

    In either case, be very careful that you don't open a security hole by either allowing more users to execute commands through the mechanism you implement, or by some user to execute commands other than the one you intend.

    1) Example of running system command using xp_cmdshell

    EXEC master..xp_CMDShell 'ISQL -L'

    2) Example of running batch file using T-SQL
    i) Running standalone batch file (without passed parameters)
    EXEC master..xp_CMDShell 'c:findword.bat'

    ii) Running parameterized batch file

    DECLARE @PassedVariable VARCHAR(100)
    SET @PassedVariable = ''
    SET @CMDSQL = 'c:findword.bat' + @PassedVariable
    EXEC master..xp_CMDShell @CMDSQL


    Monday, May 31, 2010 10:39 AM
  • PSEXEC runs (if you name a remote machine in the parameters) the desired commands on the remote machine or remote machines.  In other words, the PSEXEC.EXE runs locally, but it transmits other commands to the remote machine in order to run them there.

    From the description: "PsExec is a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software."


    Monday, May 31, 2010 12:24 PM
  • sekhara, I also observe that the OP, Chaitanya, said that the other server does not have SQL Server running on it. 


    Tuesday, June 01, 2010 8:13 PM
  • yeah... psexec could be a method to do it, but I need to copy the tool to the server where my sql job exists.
    Thursday, June 17, 2010 10:28 AM