Obtaining status information for commands invoked by xp_cmdshell

The reference page for the xp_cmdshell extended system procedure does not include the following information:

When running the xp_cmdshell extended system procedure, you can check to see whether xp_cmdshell itself was successful, as well as whether the command that xp_cmdshell invoked was successful.


To find out if xp_cmdshell itself was successful

To find out if xp_cmdshell was successful in spawning a command to the XP Server, enter:

@ret = exec xp_cmdshell command

where command is the name of the command you ran with xp_cmdshell.

Table 3: Return values and meanings of @ret = exec xp_cmdshell command

Value

What it means

0

xp_cmdshell was successful in spawning a command.

1

xp_cmdshell failed and did not spawn a command


To find out if the command invoked by xp_cmdshell was successful

To find out if the command you ran using xp_cmdshell was itself successful, enter:

@ret = exec xp_cmdshell command, return_status

where command is the name of the command you ran with xp_cmdshell.

Table 4: Return values and meanings of @ret = exec xp_cmdshell command, return_status

Value

What it means

0

The command that xp_cmdshell invoked to run in XP Server was successful.

1

The command that xp_cmdshell invoked to run in XP Server failed and was not successful.

NoteBoth @ret = exec xp_cmdshell command and @ret = exec xp_cmdshell command, return_status are backward-compatible. Old stored procedures that do not use the return_status parameter treat @ret = exec xp_cmdshell command, return_status as if it were @ret = exec xp_cmdshell command.

Also, you can still use the no_output parameter in combination with return_status, in any order.