简体   繁体   中英

How to catch exception in BCP process in SQL Server 2008?

I am using bcp in SQL Server 2008.

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

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

BCP doesn't set its return code in the way you might expect. There are a number of ways in which a BCP command may fail without the return code being set to 1.

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.

As Pondlife mentions, if you have the option of using BULK INSERT then use that instead.

Why are you using xp_cmdshell when BULK INSERT or OPENROWSET would be easier? Then you have everything in TSQL so you should be able to catch the exception. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM