简体   繁体   English

如何在SQL Server 2008中捕获BCP进程中的异常?

[英]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 INSERTOPENROWSET更容易使用时,为什么使用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.

相关问题 sql server 2008:bcp_sendrow,问题:时间格式无效 - sql server 2008: bcp_sendrow, issue: invalid time format 使用BCP导出SQL Server 2008中的存储过程结果 - using BCP to export stored procedure result in SQL Server 2008 如何在sql server中将参数传递给bcp命令 - How to pass parameter to a bcp command in sql server SQL Server 2008超时异常 - SQL Server 2008 timeout exception 在SQl服务器2008 R2中捕获log()参数的异常,该异常可能为NaN或NULL或负数 - catch an exception for argument of log() that may be a NaN or NULL or negative in SQl server 2008 R2 捕获异常并保存到SQL Server - Catch exception and save to SQL Server BCP:在SQL Server中但不在cmd中运行BCP时出错 - BCP: Error Running BCP in SQL Server but not in cmd SQL Server 2008查询到文本文件:bcp,动态SQL,临时表和表变量 - SQL Server 2008 query into text file: bcp, dynamic SQL, temp tables and table variables SQL Server 2008上的批量删除(是否有类似批量复制(bcp)的删除数据?) - Bulk DELETE on SQL Server 2008 (Is there anything like Bulk Copy (bcp) for delete data?) BCP实用程序,用于创建格式文件,将Excel数据导入SQL Server 2008以进行BULK插入 - BCP utility to create a format file, to import Excel data to SQL Server 2008 for BULK insertion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM