我不是棚里最锋利的工具,所以请耐心等待。我正在尝试将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|,| |
答案 0 :(得分:2)
问题涉及列的大小。导入功能警告在导入过程中必须进行截断,结果是丢失数据。
增加表格列的大小或删除竖线字符。
答案 1 :(得分:0)
在下一版本的SQL Server中,您也可以使用FIELDQUOTE
字符。这是介绍自SQL Server 2008以来版本的页面上的documented,可能会让休闲读者感到困惑:
BULK INSERT cands16
FROM 'C:\aaa open secrets\CampaignFin16\Cands16.txt'
WITH (FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIELDQUOTE = '|'
);
与此同时,我最好的建议是将数据加载到临时表中,然后通过删除第一个和最后一个字符将列解析为单独的列。
您可以使用'|,|'
作为字段分隔符,只更改第一列和最后一列中的值,从而简化此操作。