批量插入的问题

发布于 2024-12-12 17:43:58 字数 911 浏览 0 评论 0原文

我正在尝试将此链接中的数据插入到我的 SQL 服务器 https://www.ian.com/affiliatecenter/include/V2/CityCooperativesList.zip

我创建了表

CREATE TABLE [dbo].[tblCityCoordinatesList](
    [RegionID] [int] NOT NULL,
    [RegionName] [nvarchar](255) NULL,
    [Coordinates] [nvarchar](4000) NULL
) ON [PRIMARY]

并且我正在运行以下脚本来执行批量插入

BULK INSERT tblCityCoordinatesList
FROM 'C:\data\CityCoordinatesList.txt' 
WITH 
( 
    FIRSTROW = 2, 
    MAXERRORS = 0, 
    FIELDTERMINATOR = '|', 
    ROWTERMINATOR = '\n'
)

但是批量插入失败并出现以下错误

Cannot obtain the required interface ("IID_IColumnsInfo") from OLE DB provider "BULK" for linked server "(null)".

当我谷歌时,我发现几篇文章说问题可能与 RowTerminator 有关,但我尝试了诸如此类的所有内容\n\r、\n 等,但没有任何效果。

有人可以帮我将此数据插入我的数据库吗?

I am trying to insert the data from this link to my SQL server
https://www.ian.com/affiliatecenter/include/V2/CityCoordinatesList.zip

I created the table

CREATE TABLE [dbo].[tblCityCoordinatesList](
    [RegionID] [int] NOT NULL,
    [RegionName] [nvarchar](255) NULL,
    [Coordinates] [nvarchar](4000) NULL
) ON [PRIMARY]

And I am running the following script to do the bulk insert

BULK INSERT tblCityCoordinatesList
FROM 'C:\data\CityCoordinatesList.txt' 
WITH 
( 
    FIRSTROW = 2, 
    MAXERRORS = 0, 
    FIELDTERMINATOR = '|', 
    ROWTERMINATOR = '\n'
)

But the bulk insert fails with following error

Cannot obtain the required interface ("IID_IColumnsInfo") from OLE DB provider "BULK" for linked server "(null)".

When I google, I found several articles which says the issue may be with RowTerminator, but I tried everything like \n\r, \n etc, but nothing is working.

Could anyone please help me to insert this data into my database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

木槿暧夏七纪年 2024-12-19 17:43:58

尝试ROWTERMINATOR = '0x0a'
它应该有效。

Try ROWTERMINATOR = '0x0a'.
it should work.

情绪操控生活 2024-12-19 17:43:58

如果表和导入文件之间的列数不匹配,也会发生这种情况

This can also happen if the number of columns mismatch between the table and the imported file

嘿看小鸭子会跑 2024-12-19 17:43:58

当使用 FORMAT='CSV' 选项时,我在 SQL2019 上遇到了这个问题,并且源文件中每行的末尾都有一个逗号。因此,批量插入的表需要有一个额外的虚拟字段,以满足每个记录在源文件中本质上有一个空白字段的事实。

!CSV 文件示例

I had this on SQL2019 when the FORMAT='CSV' option was used, and there was a comma on the end of each line in the source file. So the table your BULK inserting into needed to have an extra dummy field to cater for the fact each record has essentially a blank field in the source file.

!CSV file example

淡淡の花香 2024-12-19 17:43:58

我收到了相同的错误消息,正如您所提到的,它与意外的行结束有关。
在我的例子中,行结尾在 fmt 文件中指定为 Windows 行结尾 (CRLF),写为 \r\n,并且要处理的数据文件具有 Mac 经典行结尾 (CR)。

我用一个可以显示当前行结尾并更改它的编辑器解决了这个问题。我使用 EditPad Lite 来显示打开的文件行以底部栏结尾,按下它允许替换为预期的文件行。

I got the same error message, and as you had mention, it was related to unexpected line ending.
In my case the line ending was specified in a fmt file as a Windows Line ending (CRLF), written as \r\n, and the data file to process has a Mac classic one (CR).

I solved it with an editor that can show the current line ending and change it. I used EditPad Lite wich shows the opened file line ending in the bottom bar and pressing it allow to replace with the expected one.

野却迷人 2024-12-19 17:43:58

我得到同样的错误,可能是由于文件编码问题。我通过使用 Notepad++ 打开有问题的 CSV 文件、选择所有内容并复制到剪贴板来修复此问题。接下来,创建一个新的文本文件(确保其具有 CSV 文件扩展名),使用 Notepad++ 打开它,然后将文本粘贴到新文件中。保存并关闭所有文件。您应该能够成功地将新的 CSV 文件加载到 SQL 服务器中。

I get the same error, probably from the file encoding problem. I fixed it by opening the problem CSV file using Notepad++, select everything and copy to clipboard. Next, create a new text file (making sure it has the CSV file extension), open it using Notepad++, then paste the text to the new file. Save and close all files. You should be able to successfully load the new CSV file into the SQL server.

最佳男配角 2024-12-19 17:43:58
**Filename** = C:\Users\hp\Desktop\dataset\canvas_size.csv
**File format**: 'CSV'
**Data shown as follows**

size_id,width,height,label
20,20,,20" Long Edge
24,24,,24" Long Edge
30,30,,30" Long Edge
36,36,,36" Long Edge
40,40,,40" Long Edge
48,48,,48" Long Edge
56,56,,56" Long Edge
1522,15,22,15" x 22"(38 cm x 56 cm)
1618,16,18,16" x 18"(41 cm x 46 cm)
1620,16,20,16" x 20"(41 cm x 51 cm)

**create table script**
create table canvas_size(size_id int,width int,height int,label varchar(200));

**execute select query of the table, it shows no rows.**
select * from canvas_size;

**MS SQL server bulk load script**
BULK INSERT canvas_size FROM 'C:\Users\hp\Desktop\dataset\canvas_size.csv'
   WITH (
        FORMAT = 'CSV',
        FIELDTERMINATOR = ',',
        ROWTERMINATOR = '\n',
        FIRSTROW = 2
        );
**you will get below error**
Msg 4879, Level 16, State 1, Line 18
Bulk load failed due to invalid column value in CSV data file C:\Users\hp\Desktop\dataset\canvas_size.csv in row 2, column 4.
Msg 7399, Level 16, State 1, Line 18
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 18
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".

**Solution:**
problem is if you look at the column name label data has double quotes, bulk load script what i wrote was not correct.

**Solution to modify the bulk load script**
BULK INSERT canvas_size FROM 'C:\Users\hp\Desktop\dataset\canvas_size.csv'
   WITH (
        FIELDTERMINATOR = ',',
        ROWTERMINATOR = '\n',
        FIRSTROW = 2,
        DATAFILETYPE = 'char'
        );

data is successfully loaded into table (canvas_size)

select * from canvas_size;
size_id width   height  label
20  20  NULL    20" Long Edge
24  24  NULL    24" Long Edge
30  30  NULL    30" Long Edge
36  36  NULL    36" Long Edge
40  40  NULL    40" Long Edge
48  48  NULL    48" Long Edge
56  56  NULL    56" Long Edge
1522    15  22  15" x 22"(38 cm x 56 cm)
1618    16  18  16" x 18"(41 cm x 46 cm)
1620    16  20  16" x 20"(41 cm x 51 cm)
**Filename** = C:\Users\hp\Desktop\dataset\canvas_size.csv
**File format**: 'CSV'
**Data shown as follows**

size_id,width,height,label
20,20,,20" Long Edge
24,24,,24" Long Edge
30,30,,30" Long Edge
36,36,,36" Long Edge
40,40,,40" Long Edge
48,48,,48" Long Edge
56,56,,56" Long Edge
1522,15,22,15" x 22"(38 cm x 56 cm)
1618,16,18,16" x 18"(41 cm x 46 cm)
1620,16,20,16" x 20"(41 cm x 51 cm)

**create table script**
create table canvas_size(size_id int,width int,height int,label varchar(200));

**execute select query of the table, it shows no rows.**
select * from canvas_size;

**MS SQL server bulk load script**
BULK INSERT canvas_size FROM 'C:\Users\hp\Desktop\dataset\canvas_size.csv'
   WITH (
        FORMAT = 'CSV',
        FIELDTERMINATOR = ',',
        ROWTERMINATOR = '\n',
        FIRSTROW = 2
        );
**you will get below error**
Msg 4879, Level 16, State 1, Line 18
Bulk load failed due to invalid column value in CSV data file C:\Users\hp\Desktop\dataset\canvas_size.csv in row 2, column 4.
Msg 7399, Level 16, State 1, Line 18
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 18
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".

**Solution:**
problem is if you look at the column name label data has double quotes, bulk load script what i wrote was not correct.

**Solution to modify the bulk load script**
BULK INSERT canvas_size FROM 'C:\Users\hp\Desktop\dataset\canvas_size.csv'
   WITH (
        FIELDTERMINATOR = ',',
        ROWTERMINATOR = '\n',
        FIRSTROW = 2,
        DATAFILETYPE = 'char'
        );

data is successfully loaded into table (canvas_size)

select * from canvas_size;
size_id width   height  label
20  20  NULL    20" Long Edge
24  24  NULL    24" Long Edge
30  30  NULL    30" Long Edge
36  36  NULL    36" Long Edge
40  40  NULL    40" Long Edge
48  48  NULL    48" Long Edge
56  56  NULL    56" Long Edge
1522    15  22  15" x 22"(38 cm x 56 cm)
1618    16  18  16" x 18"(41 cm x 46 cm)
1620    16  20  16" x 20"(41 cm x 51 cm)
素食主义者 2024-12-19 17:43:58

您需要从 Windows 登录(而不是从 SQL)运行 BULK INSERT - 命令。现在我没有任何例子

you need run BULK INSERT - command from windows login (not from SQL). Now I don't have any examples

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文