[英]How to catch exception in BCP process in SQL Server 2008?
I am using bcp in SQL Server 2008. 我在SQL Server 2008中使用bcp。
In some text file there is an error in data type so I am getting error message in output result but this is not raising any exception in catch block 在某些文本文件中,数据类型中存在错误,因此我在输出结果中收到错误消息,但这不会引发catch块中的任何异常
sample: 样品:
set @cmd='BCP In'
declare @xpcmdResult int
EXEC @xpcmdResult=xp_cmdShell @cmd
@xpcmdResult
should result o or 1 based upon the bcp process success @xpcmdResult
应根据bcp进程成功得到o或1
BCP doesn't set its return code in the way you might expect. BCP不会以您期望的方式设置其返回代码。 There are a number of ways in which a BCP command may fail without the return code being set to 1.
如果没有将返回码设置为1,BCP命令可能会以多种方式失败。
The most reliable way I have found of detecting BCP errors is to parse STDOUT for the text "error" or "warning", since either of these may be output without the returncode being set to 1. 我发现检测BCP错误的最可靠方法是解析文本“错误”或“警告”的STDOUT,因为这些中的任何一个都可以在没有将返回码设置为1的情况下输出。
As Pondlife mentions, if you have the option of using BULK INSERT
then use that instead. 正如Pondlife所提到的,如果您可以选择使用
BULK INSERT
那么请使用它。
Why are you using xp_cmdshell when BULK INSERT or OPENROWSET would be easier? 当BULK INSERT或OPENROWSET更容易使用时,为什么使用xp_cmdshell? Then you have everything in TSQL so you should be able to catch the exception.
然后你拥有TSQL中的所有东西,所以你应该能够捕获异常。 You should probably also review the general guidelines for bulk importing data.
您还应该查看批量导入数据的一般准则 。
An alternative would be to use SSIS, but that might be overkill if you are just importing a single text file. 另一种方法是使用SSIS,但如果您只是导入单个文本文件,那可能会有点过分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.