当出现 10 次数值超出范围错误时,BCP 文件未完全加载
BCP File not loaded completely when Numeric value out of range error occurs for 10 times
我正在尝试将制表符分隔的文件导入 SQL。 DB 中的字段之一是 decimal(15,2)
。但是我收到的文件可能有一些指数值,比如 -2.702159776E17
,这显然不适合我在数据库中的字段。
问题是如果该指数文件的记录超过 10 条,则 BCP 不会加载完整数据。一旦遇到指数值的第 10 个实例,它就会跳过文件的其余部分。
我正在使用以下 BCP 命令加载数据:
dbname.dbo.tablename in "filelocation" -c -F 2 -b 10000 -h "TABLOCK" -T -S servername
我要导入的文件有 200,000 条记录。但是我们可以清楚地看到它只处理了18000条记录,其余的都被跳过了。 BCP 的输出如下。
Starting copy...
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
18149 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 265 Average : (68486.79 rows per sec.)
查看以下内容BCP option:
-m max_errors Specifies the maximum number of syntax errors that can occur before the bcp operation is canceled. A syntax error implies a
data conversion error to the target data type. The max_errors total
excludes any errors that can be detected only at the server, such as
constraint violations.
A row that cannot be copied by the bcp utility is ignored and is
counted as one error. If this option is not included, the default is
10.
可以看到默认是10个错误。如果您想跳过所有错误,请指定此参数并提供一个非常大的值。
如果要导入所有记录,可以将记录导入到具有 FLOAT
数据类型而不是 DECIMAL(15,2)
的暂存 table。然后,通过将浮点值转换为十进制值,将数据从分段 table 复制到您想要数据的 table。
我正在尝试将制表符分隔的文件导入 SQL。 DB 中的字段之一是 decimal(15,2)
。但是我收到的文件可能有一些指数值,比如 -2.702159776E17
,这显然不适合我在数据库中的字段。
问题是如果该指数文件的记录超过 10 条,则 BCP 不会加载完整数据。一旦遇到指数值的第 10 个实例,它就会跳过文件的其余部分。
我正在使用以下 BCP 命令加载数据:
dbname.dbo.tablename in "filelocation" -c -F 2 -b 10000 -h "TABLOCK" -T -S servername
我要导入的文件有 200,000 条记录。但是我们可以清楚地看到它只处理了18000条记录,其余的都被跳过了。 BCP 的输出如下。
Starting copy...
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
SQLState = 22003, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Numeric value out of range
18149 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 265 Average : (68486.79 rows per sec.)
查看以下内容BCP option:
-m max_errors Specifies the maximum number of syntax errors that can occur before the bcp operation is canceled. A syntax error implies a data conversion error to the target data type. The max_errors total excludes any errors that can be detected only at the server, such as constraint violations.
A row that cannot be copied by the bcp utility is ignored and is counted as one error. If this option is not included, the default is 10.
可以看到默认是10个错误。如果您想跳过所有错误,请指定此参数并提供一个非常大的值。
如果要导入所有记录,可以将记录导入到具有 FLOAT
数据类型而不是 DECIMAL(15,2)
的暂存 table。然后,通过将浮点值转换为十进制值,将数据从分段 table 复制到您想要数据的 table。