all groups > sql server (alternate) > august 2007 >
You're in the

sql server (alternate)

group:

Using sql to execute a batch file


Using sql to execute a batch file mcolson
8/9/2007 3:44:36 PM
sql server (alternate):
Is it possible to use sql to execute a batch file? I would like to
execute the following "C:\BTW\bartend.exe /f=C:\BTW\Toolbox\Formats
\carnum.btw /p", 6

Thanks,

Matt
Re: Using sql to execute a batch file Roy Harvey
8/9/2007 6:26:21 PM
You can look at xp_cmdshell. Note that no program run that way can
open any sort of window, or prompt the "user" for anything at all.

You may also want to look into running it as a job.

Roy Harvey
Beacon Falls, CT

On Thu, 09 Aug 2007 15:44:36 -0000, mcolson <mcolson1590@gmail.com>
[quoted text, click to view]
Re: Using sql to execute a batch file, xp_cmdshell mcolson
8/9/2007 9:23:07 PM
[quoted text, click to view]

When I use the xp_cmdshell to try and run the bat file, i get an
output saying access denied. It should be noted that I am using sql
server 2005 express. Is this why. When I try to copy the file to
another location, i get 0 copied. I am able to dir the directory just
fine.
Re: Using sql to execute a batch file, xp_cmdshell Roy Harvey
8/10/2007 4:23:55 PM
On Thu, 09 Aug 2007 21:23:07 -0000, mcolson <mcolson1590@gmail.com>
[quoted text, click to view]

I am not familiar with Express, but I don't think it is any different
in this area. What is important to remember is that any command run
through xp_cmdshell is running under the account designated for that,
and that is the account that needs the permissions. Running
xp_cmdshell 'set' and inspecting the results should tell you the
account being used.

While it does not appear to be your problem it is worth noting also
that xp_cmdshell runs in a context without any mapped drives, so any
files on a network drive must be referenced using a UNC path of the
form \\servername\sharename\folder...

Roy Harvey
Re: Using sql to execute a batch file, xp_cmdshell Roy Harvey
8/15/2007 2:35:30 PM
On Wed, 15 Aug 2007 17:28:16 -0000, mcolson <mcolson1590@gmail.com>
[quoted text, click to view]

I have no idea, sorry.

Roy Harvey
Re: Using sql to execute a batch file, xp_cmdshell mcolson
8/15/2007 5:28:16 PM
[quoted text, click to view]

Sql is setup as a Network Service. I could change it to a local user,
but would prefer not to. I'm trying to setup a credentials named
"Matt". In here I have entered the login and password for an accepted
user of the file. How do I tell the stored procedure to use this
credential when I am running the bath file?

Thanks,

Matt
Re: Using sql to execute a batch file, xp_cmdshell Erland Sommarskog
8/15/2007 9:21:52 PM
mcolson (mcolson1590@gmail.com) writes:
[quoted text, click to view]

Have a look at xp_cmdshell and sp_xp_cmdshell_proxy_account in Books
Online. By setting up a proxy account and not run as sysadmin, you may
be able to work around the issue.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
AddThis Social Bookmark Button