请教bcp如何处理异常,想实现如下功能该如何处理?

[复制链接]
查看11 | 回复4 | 2010-10-8 09:28:51 | 显示全部楼层 |阅读模式
数据库备份,想通过bcp来做,涉及到每天自动bcp out和bcp in的问题,但有无办法判断bcp产生的异常?
如 bcpin.bat文件内容:
bcp database..table1 in ……
{在这里想判断是否成功,如果成功继续处理,失败则echo相应消息,并退出程序}
bcp database..table2 in ……
……
我想实现{}中的功能,有何办法?
bcp 有-e errfile参数,但不知道何时才产生errfile文件,因为我自己做了测试,bcp in的时候主键重复是不会产生errfile文件的?


回复

使用道具 举报

千问 | 2010-10-8 09:28:51 | 显示全部楼层







Copy in and error files
When you specify the -e error_file option with copy in, bcp stores the rows that it cannot copy in to Adaptive Server in the specified error file.
The error file stores:
A line that indicates which row failed and the error that occurred, and
A line that is an exact copy of the row in the host file.

If the file name specified after -e already exists, bcp overwrites the existing file.
If bcp does not encounter any errors, it does not create the file.

bcp in detects two types of errors:
Data conversion errors
Errors in building the row; for example, attempts to insert a NULL into columns that do not accept null values or to use invalid data formats, such as a 3-byte integer

The copy in process displays error messages on your monitor.
The following example loads the newpubs file into the publishers database, storing any error rows in the pub_err file:

bcp pubs2..publishers in newpubs -epub_err
Keep the following in mind when working with error files generated by copy in:
bcp stores rows in an error file only when the bcp program itself detects the error.
bcp continues to copy rows until bcp encounters the maximum number of error rows, at which point bcp stops the copy.
bcp sends rows to Adaptive Server in batches, so bcp cannot save copies of rows that are rejected by Adaptive Server, for example, a duplicate row for a table that has a unique index.
Adaptive Server generates error messages on a batch-by-batch basis, instead of row-by-row, and rejects the entire batch if it finds an error.
It is not considered an error for Adaptive Server to reject duplicate rows if either allow_dup_row or ignore_dup_key was set when a table's index was created. The copy proceeds normally, but the duplicate rows are neither stored in the table nor in the bcp error file.
回复

使用道具 举报

千问 | 2010-10-8 09:28:51 | 显示全部楼层
最初由 supermouse 发布
[B]数据库备份,想通过bcp来做,涉及到每天自动bcp out和bcp in的问题,但有无办法判断bcp产生的异常?
如 bcpin.bat文件内容:
bcp database..table1 in ……
{在这里想判断是否成功,如果成功继续处理,失败则echo相应消息,并退出程序}
bcp database..table2 in ……
……
我想实现{}中的功能,有何办法?
bcp 有-e errfile参数,但不知道何时才产生errfile文件,因为我自己做了测试,bcp in的时候主键重复是不会产生errfile文件的?

[/B]

解决问题 的思路好象有问题,为什么不用复制?
回复

使用道具 举报

千问 | 2010-10-8 09:28:51 | 显示全部楼层
bcp database..table1 in ……
{在这里想判断是否成功,如果成功继续处理,失败则echo相应消息,并退出程序}
bcp database..table1 in ……
if not "%errorlevel%"=="0" (
echo 相应消息>errorfile
exit
)
回复

使用道具 举报

千问 | 2010-10-8 09:28:51 | 显示全部楼层
我的使用经验,不一定一样,仅供参考。
通过ADO在数据库中调用bcp,bcp的执行结果会作为结果集返回。
在其中查找error关键字,如果存在则报错。
如果你是在终端执行,可以考虑 bcp ...... >> EXECLOG.txt (有的终端命令不支持,不知道bcp行不行)
然后在EXECLOG.txt里面查找关键字。
另外如果SQL Server和Sybase数据库共存时bcp调用会有冲突。如果你遇到了说一声。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行