ca9163d9
ca9163d9

Reputation: 29159

How to raise exception from command run by xp_cmdshell?

The following code uses xp_cmdshell to execute bcp.exe. However, it continues to run the following code even if the command/executable failed. How to raise an exception if the execution (my special case is running sybase bcp) didn't succeeded?

declare @cmd varchar(max) = 'c:\sybase\bcp.exe ....'
begin try
  exec xp_cmdshell @cmd
  // .... do something if there is no error when executing @cmd
  // Otherwise, STOP!
end try
begin catch
  // report error
end catch

Upvotes: 0

Views: 715

Answers (2)

Micah N
Micah N

Reputation: 51

Below is an example from here that might help...

enter image description here

Upvotes: 2

benjamin moskovits
benjamin moskovits

Reputation: 5458

xp_cmdshell will (if the invoked program cooperates) return either a 0 if the program ended successfully or Null if not. I don't think try catch will be affected either way.

Upvotes: 0

Related Questions