MS SQL Server 2019 OpenRowSet 使用 CSV 文件批量插入 -(0 行受影响)

发布于 2025-01-12 06:05:46 字数 2032 浏览 1 评论 0原文

我正在尝试使用 OPENROWSET BULK INSERT 将 CSV 文件加载到 MS SQL Server 上,并且正在插入 0 行。

我在 SQL Server Management Studio 中使用以下代码;


-- DROP TABLE finances.dbo.MasterCard_Import_CSV;

CREATE TABLE finances.dbo.MasterCard_Import_CSV (
    Date datetime
    , Type varchar(10)
    , Description varchar(100)
    , Value float
    , Balance float
    , Name varchar(20)
    , Account varchar(50));

INSERT INTO finances.dbo.MasterCard_Import_CSV
    SELECT * FROM
    OPENROWSET(
        BULK 'C:\dev\workspace\Portfolio\DataAnalyst_2_Finances\RawData\BJKEOGH-MASTERCARD.csv'
        , FORMATFILE = 'C:\dev\workspace\Portfolio\DataAnalyst_2_Finances\OpenRowSet_BulkImport_Scripts\MASTERCARD_csv_file_import_set.txt'
        , FIRSTROW=2
        , FORMAT='CSV'
    ) AS TMP

格式文件的内容如下(我在文件底部添加了一个空行);

15.0
7
1 SQLCHAR 0 10 ',' 1 Date SQL_LATIN1_General_CP1_CI_AI
2 SQLCHAR 0 10 ',' 2 Type SQL_LATIN1_General_CP1_CI_AI
3 SQLCHAR 0 100 ',' 3 Description SQL_LATIN1_General_CP1_CI_AI
4 SQLCHAR 0 10 ',' 4 Value SQL_LATIN1_General_CP1_CI_AI
5 SQLCHAR 0 10 ',' 5 Balance SQL_LATIN1_General_CP1_CI_AI
6 SQLCHAR 0 20 ',' 6 Name SQL_LATIN1_General_CP1_CI_AI
7 SQLCHAR 0 50 '\r\n' 7 Account SQL_LATIN1_General_CP1_CI_AI

CSV 文件示例如下;

Date, Type, Description, Value, Balance, Account Name, Account
09-Nov-21,Purchase,'AMZNMKTPLACE AMAZON.CO AMAZON.CO.UK GBR,17.99,,'B J KEOGH,'121212******1212
09-Nov-21,Purchase,'MARKS AND SPENCER PLC BIRMINGHAM GBR,10.85,,'B J KEOGH,'121212******1212
10-Nov-21,Purchase,'DVA LICENSING COLERAINE GBR,30,,'B J KEOGH,'121212******1212
10-Nov-21,Purchase,'TESCO STORES 5392 BIRMINGHAM GBR,1.3,,'B J KEOGH,'121212******1212
11-Nov-21,Purchase,'TESCO STORES 6227 HOCKLEY GBR,13.9,,'B J KEOGH,'121212******1212
12-Nov-21,Purchase,'Prime Video*QR1EA1PT5 353-12477661 GBR,7.99,,'B J KEOGH,'121212******1212

我错过了什么吗?

I am attempting to load a CSV file onto MS SQL Server with OPENROWSET BULK INSERT and 0 rows are being inserted.

I am using the following code within SQL Server Management Studio;


-- DROP TABLE finances.dbo.MasterCard_Import_CSV;

CREATE TABLE finances.dbo.MasterCard_Import_CSV (
    Date datetime
    , Type varchar(10)
    , Description varchar(100)
    , Value float
    , Balance float
    , Name varchar(20)
    , Account varchar(50));

INSERT INTO finances.dbo.MasterCard_Import_CSV
    SELECT * FROM
    OPENROWSET(
        BULK 'C:\dev\workspace\Portfolio\DataAnalyst_2_Finances\RawData\BJKEOGH-MASTERCARD.csv'
        , FORMATFILE = 'C:\dev\workspace\Portfolio\DataAnalyst_2_Finances\OpenRowSet_BulkImport_Scripts\MASTERCARD_csv_file_import_set.txt'
        , FIRSTROW=2
        , FORMAT='CSV'
    ) AS TMP

The contents of the format file are as follows (i have added a blank line to the bottom of the file);

15.0
7
1 SQLCHAR 0 10 ',' 1 Date SQL_LATIN1_General_CP1_CI_AI
2 SQLCHAR 0 10 ',' 2 Type SQL_LATIN1_General_CP1_CI_AI
3 SQLCHAR 0 100 ',' 3 Description SQL_LATIN1_General_CP1_CI_AI
4 SQLCHAR 0 10 ',' 4 Value SQL_LATIN1_General_CP1_CI_AI
5 SQLCHAR 0 10 ',' 5 Balance SQL_LATIN1_General_CP1_CI_AI
6 SQLCHAR 0 20 ',' 6 Name SQL_LATIN1_General_CP1_CI_AI
7 SQLCHAR 0 50 '\r\n' 7 Account SQL_LATIN1_General_CP1_CI_AI

A sample of the CSV file is as follows;

Date, Type, Description, Value, Balance, Account Name, Account
09-Nov-21,Purchase,'AMZNMKTPLACE AMAZON.CO AMAZON.CO.UK GBR,17.99,,'B J KEOGH,'121212******1212
09-Nov-21,Purchase,'MARKS AND SPENCER PLC BIRMINGHAM GBR,10.85,,'B J KEOGH,'121212******1212
10-Nov-21,Purchase,'DVA LICENSING COLERAINE GBR,30,,'B J KEOGH,'121212******1212
10-Nov-21,Purchase,'TESCO STORES 5392 BIRMINGHAM GBR,1.3,,'B J KEOGH,'121212******1212
11-Nov-21,Purchase,'TESCO STORES 6227 HOCKLEY GBR,13.9,,'B J KEOGH,'121212******1212
12-Nov-21,Purchase,'Prime Video*QR1EA1PT5 353-12477661 GBR,7.99,,'B J KEOGH,'121212******1212

Am I missing something?

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

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

发布评论

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

评论(1

峩卟喜欢 2025-01-19 06:05:46

问题是你的格式文件。删除单引号,INSERT 工作正常:

15.0
7
1 SQLCHAR 0 0 , 1 Date SQL_LATIN1_General_CP1_CI_AI
2 SQLCHAR 0 0 , 2 Type SQL_LATIN1_General_CP1_CI_AI
3 SQLCHAR 0 0 , 3 Description SQL_LATIN1_General_CP1_CI_AI
4 SQLCHAR 0 0 , 4 Value SQL_LATIN1_General_CP1_CI_AI
5 SQLCHAR 0 0 , 5 Balance SQL_LATIN1_General_CP1_CI_AI
6 SQLCHAR 0 0 , 6 Name SQL_LATIN1_General_CP1_CI_AI
7 SQLCHAR 0 0 \r\n 7 Account SQL_LATIN1_General_CP1_CI_AI

一旦我这样做了,你的数据就可以毫无问题地打开。

演示屏幕截图文件读取正确。

The problem is your format file. Remove the single quotes, and the INSERT works fine:

15.0
7
1 SQLCHAR 0 0 , 1 Date SQL_LATIN1_General_CP1_CI_AI
2 SQLCHAR 0 0 , 2 Type SQL_LATIN1_General_CP1_CI_AI
3 SQLCHAR 0 0 , 3 Description SQL_LATIN1_General_CP1_CI_AI
4 SQLCHAR 0 0 , 4 Value SQL_LATIN1_General_CP1_CI_AI
5 SQLCHAR 0 0 , 5 Balance SQL_LATIN1_General_CP1_CI_AI
6 SQLCHAR 0 0 , 6 Name SQL_LATIN1_General_CP1_CI_AI
7 SQLCHAR 0 0 \r\n 7 Account SQL_LATIN1_General_CP1_CI_AI

Once I did that, your data was opened without issue.

Screenshot demonstrating the file was read correctly.

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