xp_cmdshell and pass command line parameters to .exe file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Bart

    xp_cmdshell and pass command line parameters to .exe file

    I need to execute xp_cmdshell in a trigger and pass a command line
    parameter to the .exe program

    i.e. I have a .exe program c:\program files\savedata. exe

    In the trigger I have a parameter @Id bigint

    I need to pass the parameter @Id to savedata.exe

    In the trigger, I am trying

    EXEC master..xp_cmds hell 'cmd.exe /C "c:\program files\savedata. exe "'
    + CAST(@Id as varchar)

    BUT this does not work.

    Can someone please help me with how to do this?

    Thanks,

    db
  • Simon Hayes

    #2
    Re: xp_cmdshell and pass command line parameters to .exe file

    dblist2003@yaho o.com (Dan Bart) wrote in message news:<fa87ff30. 0311101329.10b9 d3fd@posting.go ogle.com>...[color=blue]
    > I need to execute xp_cmdshell in a trigger and pass a command line
    > parameter to the .exe program
    >
    > i.e. I have a .exe program c:\program files\savedata. exe
    >
    > In the trigger I have a parameter @Id bigint
    >
    > I need to pass the parameter @Id to savedata.exe
    >
    > In the trigger, I am trying
    >
    > EXEC master..xp_cmds hell 'cmd.exe /C "c:\program files\savedata. exe "'
    > + CAST(@Id as varchar)
    >
    > BUT this does not work.
    >
    > Can someone please help me with how to do this?
    >
    > Thanks,
    >
    > db[/color]

    Calling an external program from a trigger is usually a bad idea - if
    the external program takes a long time to run, hangs, or does
    something unexpected, you can easily block other clients. A better
    plan is to insert your @Id values into a separate table, then have a
    scheduled job which processes the values at regular intervals - this
    won't block or otherwise impact the main processing.

    If you really want to do it, you should rewrite your command like
    this:

    declare @cmd varchar(8000)
    set @cmd = 'cmd.exe /C "c:\program files\savedata. exe "' + CAST(@Id as
    varchar)
    EXEC master..xp_cmds hell @cmd

    Simon

    Comment

    Working...