BCP - 导入到具有 IDENTITY 列的 table 时,强制转换规范的字符值无效

BCP - Invalid character value for cast specification when importing to table with IDENTITY column

我正在尝试将文件从 UNIX 加载到 SQL 服务器。当我 运行 BCP 收到以下错误时,但当我将数据类型更改为 Char 时,我能够加载相同的文件。

使用的命令:

--bcp [SQLAAA].[APP_XXX].[ACTIVITY_V1] in /home/XXX_ACTIVITY.txt -c -S xddb001 -e /home/ERRORS.log -t "|" -r "\n" -U test12 -P test12

源文件:

222|2017-12-27|Y|ABC|2017-12-27|ABC|2017-12-27
255|2018-01-04|N|ABC|2018-01-04|ABC|2018-01-04

错误信息:

@ Row 1, Column 7: Invalid character value for cast specification @

定义table:

CREATE TABLE [APP_XX].[ACTIVITY]
(
[C1] [varchar](10) NOT NULL,
[C2_DATE] [date] NOT NULL,
[c3] [varchar](1) NULL,
[C4_NM] [varchar](10) NULL,
[C5_DTM] [date] NULL,
[C6_NM] [varchar](10) NULL,
[C7_DTM] [date] NULL,
[ROW_ID] [bigint] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]

请问可以做哪些修改?

当导入到带有 IDENTITY 列的 table 中,并且您没有为导入文件中的 IDENTITY 列提供值时,您需要在格式文件中指定将跳过 IDENTITY 列导入。

Microsoft documentation on BCP, flag -E:

中明确提到了这种工作方式

If the data file does not contain values for the identity column in the table or view, use a format file to specify that the identity column in the table or view should be skipped when importing data; SQL Server automatically assigns unique values for the column.[...]

您需要做的事情:

  1. 首先create a format file using BCP。例如。对于非 XML 格式的文件,命令看起来像 BCP table_or_view format nul -f format_file_name
  2. 指定跳过格式文件中的 IDENTITY 列
  3. 使用您在 BCP IN 命令中使用 BCP -f flag
  4. 创建的格式文件