Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2009-02-13
Medium Priority
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 to@test.com -f from@test.com -s test -m

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

set @cmdtest = 'c:\sendmail.exe -t to@test.com -f from@test.com -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
  • 2
LVL 15

Assisted Solution

MohammedU earned 600 total points
ID: 23635736
Try wrapping the double quote around the command...

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

Author Comment

ID: 23635778
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

BrandonGalderisi earned 600 total points
ID: 23635896

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

Accepted Solution

TimFCollins earned 0 total points
ID: 23639420
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'+' '+'to@test.com'+' '+'-f'+' '+'from@test.com'+' '+'-s'+' '+'test'+' '+'=m'+' '+''
exec xp_cmdshell@cmdtest

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

Tim F.

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

578 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