如何从xp_cmdshell运行的命令引发异常?

时间:2015-02-27 19:50:13

标签: sql-server

以下代码使用xp_cmdshell执行bcp.exe。但是,即使命令/可执行文件失败,它仍继续运行以下代码。如果执行(我的特殊情况是运行sybase bcp)没有成功,如何引发异常?

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

2 个答案:

答案 0 :(得分:2)

以下是here中可能有帮助的示例......

enter image description here

答案 1 :(得分:0)

xp_cmdshell将(如果被调用的程序合作)如果程序成功结束则返回0,否则返回Null。我不认为尝试捕获会受到任何影响。