从 CSV 批量插入 - 国家代码不会出现 SQL table
Bulk insert from CSV - country codes are not coming to SQL table
我有一个简单的 CSV 文件,其中包含 60 行数据,如下所示:
"Name","City","Region","Primary Phone","Postal Code"
"MyCompany Inc.","Toronto","GTA","+1 (416) 999-9999","M5G 1Z8"
...
在我的 SQL 查询中,我使用 bulk insert
读取文件,如下所示:
CREATE TABLE #RegionalOffices (
[REIT] [NVARCHAR](255) NULL,
[City] [NVARCHAR](255) NULL,
[Region] [NVARCHAR](255) NULL,
[PrimaryPhone] [NVARCHAR](255) NULL,
[PostalCode] [NVARCHAR](255) NULL
)
BULK INSERT #RegionalOffices
FROM 'C:\Scripts\regionaloffices.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
CODEPAGE = '65000',
TABLOCK
)
这是我的数据在 table 中的显示方式,当我这样做时 select *
:
Name City Region PrimaryPhone PostalCode
"MyCompany Inc." "Toronto" "GTA" "(416) 999-9999" "M5G 1Z8"
我的问题是: csv 文件中的 table 没有出现国家代码“+1”,其他行也是如此,我没有看看为什么会这样。任何帮助将不胜感激。
它对我有用。
我修改了CODEPAGE = '65001'
从 SQL Server 2017 开始,多了一个参数 BULK INSERT
参数 FORMAT='CSV'
。它删除了每列的双引号分隔符。
SQL
USE tempdb;
GO
DROP TABLE IF EXISTS dbo.RegionalOffices;
CREATE TABLE dbo.RegionalOffices
(
[REIT] [NVARCHAR](255) NULL,
[City] [NVARCHAR](255) NULL,
[Region] [NVARCHAR](255) NULL,
[PrimaryPhone] [NVARCHAR](255) NULL,
[PostalCode] [NVARCHAR](255) NULL
);
BULK INSERT dbo.RegionalOffices
FROM 'e:\temp\regionaloffices.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
CODEPAGE = '65001',
TABLOCK
)
-- test
SELECT * FROM dbo.RegionalOffices;
输出
+------------------+-----------+--------+---------------------+------------+
| REIT | City | Region | PrimaryPhone | PostalCode |
+------------------+-----------+--------+---------------------+------------+
| "MyCompany Inc." | "Toronto" | "GTA" | "+1 (416) 999-9999" | "M5G 1Z8" |
+------------------+-----------+--------+---------------------+------------+
我有一个简单的 CSV 文件,其中包含 60 行数据,如下所示:
"Name","City","Region","Primary Phone","Postal Code"
"MyCompany Inc.","Toronto","GTA","+1 (416) 999-9999","M5G 1Z8"
...
在我的 SQL 查询中,我使用 bulk insert
读取文件,如下所示:
CREATE TABLE #RegionalOffices (
[REIT] [NVARCHAR](255) NULL,
[City] [NVARCHAR](255) NULL,
[Region] [NVARCHAR](255) NULL,
[PrimaryPhone] [NVARCHAR](255) NULL,
[PostalCode] [NVARCHAR](255) NULL
)
BULK INSERT #RegionalOffices
FROM 'C:\Scripts\regionaloffices.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
CODEPAGE = '65000',
TABLOCK
)
这是我的数据在 table 中的显示方式,当我这样做时 select *
:
Name City Region PrimaryPhone PostalCode
"MyCompany Inc." "Toronto" "GTA" "(416) 999-9999" "M5G 1Z8"
我的问题是: csv 文件中的 table 没有出现国家代码“+1”,其他行也是如此,我没有看看为什么会这样。任何帮助将不胜感激。
它对我有用。
我修改了CODEPAGE = '65001'
从 SQL Server 2017 开始,多了一个参数 BULK INSERT
参数 FORMAT='CSV'
。它删除了每列的双引号分隔符。
SQL
USE tempdb;
GO
DROP TABLE IF EXISTS dbo.RegionalOffices;
CREATE TABLE dbo.RegionalOffices
(
[REIT] [NVARCHAR](255) NULL,
[City] [NVARCHAR](255) NULL,
[Region] [NVARCHAR](255) NULL,
[PrimaryPhone] [NVARCHAR](255) NULL,
[PostalCode] [NVARCHAR](255) NULL
);
BULK INSERT dbo.RegionalOffices
FROM 'e:\temp\regionaloffices.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
CODEPAGE = '65001',
TABLOCK
)
-- test
SELECT * FROM dbo.RegionalOffices;
输出
+------------------+-----------+--------+---------------------+------------+
| REIT | City | Region | PrimaryPhone | PostalCode |
+------------------+-----------+--------+---------------------+------------+
| "MyCompany Inc." | "Toronto" | "GTA" | "+1 (416) 999-9999" | "M5G 1Z8" |
+------------------+-----------+--------+---------------------+------------+