Using xp_cmdshell

  • Hi All

    I am trying to use xp_cmdshell to run a batch file but I am not having much luck.

    How do I change the working directory and then call the batch file?

    Thanks

    Craig 🙂

  • why not just call the file directly? why do you have to do the cd command, when you know where the file is anyway?

    create table #Results (

    ID int identity(1,1) NOT NULL,

    TheOutput varchar(1000))

    insert into #Results (TheOutput)

    exec master..xp_cmdshell 'c:\SomeFolder\MyCommandfile.bat'

    select * from #Results

    make sure the command you run doesn't return any prompts (do you want to delete Y/N etc) or it will hang awaiting input.

    also don't forget that even if YOU can go to the command prompt and run the command, SQL uses a different set of security credentials when accessing anything outside of SQL server; make sure the account SQL runs under has access to the directory/share you are trying to access:

    SQL Server uses either the account set up as the proxy account, or if that is left blank(the default) it uses account it starts with to try and access the resource:

    or if the above was blank, the account in services:(or these credentials if you are sysadmin)

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply