导入包含管道字符的 CSV 文件。错误说截断
Importing a CSV file that has pipe characters. Error says truncation
我不是棚子里最锋利的工具,所以请多多包涵。我正在尝试将 csv 文件导入 Microsoft SQL Server 2016。但是,每当我尝试这样做时,我都会遇到错误。我认为这可能与管道字符有关。我从中获取文件的网站是这样说的:
文本字段被竖线字符 (ascii 124) 包围。日期和数字字段不是。逗号分隔所有字段。
这是我的查询:
CREATE TABLE cands16 (
[Cycle] [char](4) NOT NULL,
[FECCandID] [char] (9) NOT NULL,
[CID] [char] (9) NULL,
[FirstLastP] [varchar] (50) NULL,
[Party] [char] (1) NULL,
[DistIDRunFor] [char] (4) NULL,
[DistIDCurr] [char] (4) NULL,
[CurrCand] [char] (1) NULL,
[CRPICO] [char] (1) NULL,
[RecipCode] [char] (2) NULL,
[NoPacs] [char] (1) NULL
) ON [PRIMARY]
BULK
INSERT cands16
FROM 'C:\aaa open secrets\CampaignFin16\Cands16.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
这是我的错误:
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 1, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 2, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 3, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 4, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 5, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 6, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 7, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 8, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 9, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 10, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 11, column 1 (Cycle).
Msg 4865, Level 16, State 1, Line 19
Cannot bulk load because the maximum number of errors (10) was exceeded.
Msg 7399, Level 16, State 1, Line 19
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 19
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
这是我的 CSV 文件的婴儿版本:
|2016|,|H4GA02060|,|N00035294|,|Greg Duke (R)|,|R|,|GA02|,| |,|Y|,|Y|,|C|,|RC|,| |
|2016|,|H4GA02078|,|N00036257|,|Vivian Childs (R)|,|R|,|GA02|,| |,| |,| |,| |,|RN|,| |
|2016|,|H4GA04116|,|N00035798|,|Thomas Brown (D)|,|D|,|GA04|,| |,| |,| |,| |,|DN|,| |
|2016|,|H4GA04124|,|N00035862|,|Thomas Wight (D)|,|D|,|GA07|,| |,| |,| |,| |,|DN|,| |
|2016|,|H4GA06087|,|N00026160|,|Tom Price (R)|,|R|,|GA06|,|GA06|,|Y|,|Y|,|I|,|RW|,| |
|2016|,|H4GA08067|,|N00026163|,|Lynn A Westmoreland (R)|,|R|,|GA03|,|GA03|,| |,|Y|,|I|,|RI|,| |
|2016|,|H4GA09065|,|N00036258|,|Bernard Fontaine (R)|,|R|,|GA09|,| |,| |,|Y|,|C|,|RL|,| |
|2016|,|H4GA10071|,|N00035370|,|Mike Collins (R)|,|R|,|GA10|,| |,| |,| |,| |,|RN|,| |
|2016|,|H4GA11046|,|N00035321|,|Susan Davis (R)|,|R|,|GA11|,| |,| |,| |,| |,|RN|,| |
在下个版本的SQL服务器中,你也可以使用FIELDQUOTE
字符了。这是 documented 在描述自 SQL Server 2008 以来的版本的页面上,可能会让普通读者感到困惑:
BULK INSERT cands16
FROM 'C:\aaa open secrets\CampaignFin16\Cands16.txt'
WITH (FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIELDQUOTE = '|'
);
与此同时,我最好的建议是将数据加载到分段 table,然后通过删除第一个和最后一个字符将列解析为单独的列。
您可以通过使用 '|,|'
作为字段分隔符并仅更改第一列和最后一列中的值来稍微简化一下。
问题与列的大小有关。导入功能警告在导入过程中必须进行截断,结果会丢失数据。
增加 table 列的大小或删除管道字符。
我不是棚子里最锋利的工具,所以请多多包涵。我正在尝试将 csv 文件导入 Microsoft SQL Server 2016。但是,每当我尝试这样做时,我都会遇到错误。我认为这可能与管道字符有关。我从中获取文件的网站是这样说的:
文本字段被竖线字符 (ascii 124) 包围。日期和数字字段不是。逗号分隔所有字段。
这是我的查询:
CREATE TABLE cands16 (
[Cycle] [char](4) NOT NULL,
[FECCandID] [char] (9) NOT NULL,
[CID] [char] (9) NULL,
[FirstLastP] [varchar] (50) NULL,
[Party] [char] (1) NULL,
[DistIDRunFor] [char] (4) NULL,
[DistIDCurr] [char] (4) NULL,
[CurrCand] [char] (1) NULL,
[CRPICO] [char] (1) NULL,
[RecipCode] [char] (2) NULL,
[NoPacs] [char] (1) NULL
) ON [PRIMARY]
BULK
INSERT cands16
FROM 'C:\aaa open secrets\CampaignFin16\Cands16.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
这是我的错误:
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 1, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 2, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 3, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 4, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 5, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 6, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 7, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 8, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 9, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 10, column 1 (Cycle).
Msg 4863, Level 16, State 1, Line 19
Bulk load data conversion error (truncation) for row 11, column 1 (Cycle).
Msg 4865, Level 16, State 1, Line 19
Cannot bulk load because the maximum number of errors (10) was exceeded.
Msg 7399, Level 16, State 1, Line 19
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 19
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
这是我的 CSV 文件的婴儿版本:
|2016|,|H4GA02060|,|N00035294|,|Greg Duke (R)|,|R|,|GA02|,| |,|Y|,|Y|,|C|,|RC|,| |
|2016|,|H4GA02078|,|N00036257|,|Vivian Childs (R)|,|R|,|GA02|,| |,| |,| |,| |,|RN|,| |
|2016|,|H4GA04116|,|N00035798|,|Thomas Brown (D)|,|D|,|GA04|,| |,| |,| |,| |,|DN|,| |
|2016|,|H4GA04124|,|N00035862|,|Thomas Wight (D)|,|D|,|GA07|,| |,| |,| |,| |,|DN|,| |
|2016|,|H4GA06087|,|N00026160|,|Tom Price (R)|,|R|,|GA06|,|GA06|,|Y|,|Y|,|I|,|RW|,| |
|2016|,|H4GA08067|,|N00026163|,|Lynn A Westmoreland (R)|,|R|,|GA03|,|GA03|,| |,|Y|,|I|,|RI|,| |
|2016|,|H4GA09065|,|N00036258|,|Bernard Fontaine (R)|,|R|,|GA09|,| |,| |,|Y|,|C|,|RL|,| |
|2016|,|H4GA10071|,|N00035370|,|Mike Collins (R)|,|R|,|GA10|,| |,| |,| |,| |,|RN|,| |
|2016|,|H4GA11046|,|N00035321|,|Susan Davis (R)|,|R|,|GA11|,| |,| |,| |,| |,|RN|,| |
在下个版本的SQL服务器中,你也可以使用FIELDQUOTE
字符了。这是 documented 在描述自 SQL Server 2008 以来的版本的页面上,可能会让普通读者感到困惑:
BULK INSERT cands16
FROM 'C:\aaa open secrets\CampaignFin16\Cands16.txt'
WITH (FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIELDQUOTE = '|'
);
与此同时,我最好的建议是将数据加载到分段 table,然后通过删除第一个和最后一个字符将列解析为单独的列。
您可以通过使用 '|,|'
作为字段分隔符并仅更改第一列和最后一列中的值来稍微简化一下。
问题与列的大小有关。导入功能警告在导入过程中必须进行截断,结果会丢失数据。
增加 table 列的大小或删除管道字符。