SQL 服务器批量插入:为什么 "The FIRSTROW attribute is not intended to skip column headers"?
SQL Server BULK INSERT: why "The FIRSTROW attribute is not intended to skip column headers"?
这里
https://msdn.microsoft.com/en-us/library/ms188365.aspx
我可以读到:
The FIRSTROW attribute is not intended to skip column headers.
Skipping headers is not supported by the BULK INSERT statement. When
skipping rows, the SQL Server Database Engine looks only at the field
terminators, and does not validate the data in the fields of skipped
rows.
但我确实想这样做。为什么这不是故意的?当我使用 FIRSTROW = 2 跳过 headers 时会出现任何问题吗?
它会工作正常,但它需要第一行是相同的格式(意味着 header 必须具有相同的 FIELDTERMINATOR 和 ROWTERMINATOR)并且当使用 firstrow = 2 时,数据将从第二行。
例如,如果我有 CSV 文件要导入什么,第一行是列 header,我的代码将像这样
BULK INSERT myTable
FROM 'C:\myTable.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
the SQL Server Database Engine looks only at the field terminators
这就是你的答案。如果您的列 header 的名称中包含字段终止符,系统将在第一行中找到错误的字段数,从而导致大笑。你可以想象,在世界上所有的文件中,某个列有
Years, since 1970
作为 header。人类足够清楚,但机器只有 规则 。
可能您不知道,对于 CSV 文件,您的 bulk insert
通常会失败。 CSV 格式非常多变,并且比 bulk insert
可以正确解释的格式更复杂。具体来说,存在允许在数据中包含逗号的引用规则。 SQL 服务器不接受引号。无论如何,它将把逗号解释为分隔符。就像男人说的,
the SQL Server Database Engine looks only at the field terminators
这里 https://msdn.microsoft.com/en-us/library/ms188365.aspx 我可以读到:
The FIRSTROW attribute is not intended to skip column headers. Skipping headers is not supported by the BULK INSERT statement. When skipping rows, the SQL Server Database Engine looks only at the field terminators, and does not validate the data in the fields of skipped rows.
但我确实想这样做。为什么这不是故意的?当我使用 FIRSTROW = 2 跳过 headers 时会出现任何问题吗?
它会工作正常,但它需要第一行是相同的格式(意味着 header 必须具有相同的 FIELDTERMINATOR 和 ROWTERMINATOR)并且当使用 firstrow = 2 时,数据将从第二行。 例如,如果我有 CSV 文件要导入什么,第一行是列 header,我的代码将像这样
BULK INSERT myTable
FROM 'C:\myTable.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
the SQL Server Database Engine looks only at the field terminators
这就是你的答案。如果您的列 header 的名称中包含字段终止符,系统将在第一行中找到错误的字段数,从而导致大笑。你可以想象,在世界上所有的文件中,某个列有
Years, since 1970
作为 header。人类足够清楚,但机器只有 规则 。
可能您不知道,对于 CSV 文件,您的 bulk insert
通常会失败。 CSV 格式非常多变,并且比 bulk insert
可以正确解释的格式更复杂。具体来说,存在允许在数据中包含逗号的引用规则。 SQL 服务器不接受引号。无论如何,它将把逗号解释为分隔符。就像男人说的,
the SQL Server Database Engine looks only at the field terminators