我正在尝试将包含这些内容的CSV导入我的SQL Server数据库。
数据:
tranche_d_age
<18
18_24
25_34
35_44
45_60
>60
格式文件:
14.0
1
1 SQLCHAR 0 0 "\n" 2 tranche_d_age_id French_CI_AS
在数据库中,我创建了一个具有以下规范的表:
DROP TABLE IF EXISTS [dbo].[ref_tranches_age]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ref_tranches_age](
[id] [int] IDENTITY(1,1) NOT NULL,
[tranche_d_age_id] [varchar](max) NULL
)
GO
这是我发出的bcp命令:
bcp [xxx].dbo.tranches_age in 'ref_tranches_age.csv' -U 'xxx' -P 'xxx' -S 'xxx' -f bcp_ref_tranches_age.fmt
响应:
Starting copy...
SQLState = 22005, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Invalid character value for cast specification
SQLState = 22005, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Invalid character value for cast specification
SQLState = 22005, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Invalid character value for cast specification
SQLState = 22005, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Invalid character value for cast specification
SQLState = 22005, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Invalid character value for cast specification
SQLState = 22005, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Invalid character value for cast specification
SQLState = 22005, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Invalid character value for cast specification
0 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 22
在我看来,响应包含csv文件中7行的7条错误消息。 (我可能是错的)
我发现这2个线程说我的源文件中不应有空格,也不应插入标识列。除非我错了,否则我不会这么做。
[创建文件ref_tranches_age.csv时,可能包含lineterminator ='\ r \ n'作为行分隔符,文件bcp_ref_tranches_age.fmt则指定lineterminator ='\ n'。因此,您有两个选择,第一个是在创建csv文件时,指定lineterminator ='\ n'和voila,或者在bcp_ref_tranches_age.fmt文件中添加lineterminator ='\ r \ n',如下所示:
bcp [xxx] .dbo.tranches_age in 'ref_tranches_age.csv' -U 'xxx' -P 'xxx' -S 'xxx' -f bcp_ref_tranches_age.fmt -r "\ r \ n"
。fmt文件将如下所示:
14.0
1
1
SQLCHAR 0 0 "\r\n" 2 tranche_d_age_id French_CI_AS