How can I configure the xp_cmdshell command to execute a file through the command prompt including passing variables?

Posted on 2009-02-13
Last Modified: 2012-05-06

I am trying to use the xp_cmdshell sp in SQL Server 2000 to execute an exe file that requires variables.  I can get it to execute the file but it won't add the variables.

I am trying to call sendmail.exe.  When in a command window I can type:
c:\sendmail.exe -t -f -s test -m

and that works fine.  However, when I put it in a SQL Server stored procedure:

set @cmdtest = 'c:\sendmail.exe -t -f -s test -m'
exec xp_cmdshell @cmdtest

when i run this i get a result back that is the same as entering 'c:\sendmail.exe 'in a command window.  How can I get SQL Server to recognize the rest of the string that I'm passing to xp_cmdshell?

Thanks in Advance,
Tim F.
Question by:TimFCollins
    LVL 15

    Assisted Solution

    Try wrapping the double quote around the command...

    set @cmdtest = 'c:\"sendmail.exe -t -f -s test -m"'
    if didn't work then
    Try wrapping each parameter in double quote....

    Author Comment

    Thanks for the quick response.  I tried it with the double quotes like you suggested and also around each parameter and I got back that it is not recognized as an internal or external command, operable file or batch file.  Is there anything else that might make it recognize the string as a whole?

    Tim F.
    LVL 39

    Assisted Solution


    set @cmdtest = '"c:\sendmail.exe -t -f -s test -m"'
    exec xp_cmdshell @cmdtest

    Accepted Solution

    Brandon - I tried your solution as well and it still didn't work.  I played around with it a bunch of different ways and finally it worked (not sure why this did and the others didn't, doesn't really make sense to me....)

    set @cmdtest = 'c:\sendmail.exe'+' '+'-t'+' '+''+' '+'-f'+' '+''+' '+'-s'+' '+'test'+' '+'=m'+' '+''
    exec xp_cmdshell@cmdtest

    Thank you both for your help - I'm glad it's working now.

    Tim F.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I recently came across an interesting Question In EE ( and was puzzled about how to achieve that using SSIS out of the box tasks, which was i…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now