SQL 服务器批量插入 EDI 文件 - 行顺序混乱
SQL Server Bulk Insert EDI file - row order jumbled
我正在使用 SQL 服务器批量插入命令将带有终止符的 EDI 文件内容插入到我的 table 中,但是,有时顺序或行会出现混乱,例如:
BULK INSERT dbo.NDCArchitectBulkInsert
FROM '\FTP\TestFiles\abc'
WITH (ROWTERMINATOR = '~')
文件(abc)内容:
Line 139 This is line 139~
Line 140 This is line 140~
...
Line 149 This is line 149~
Line 150 This is line 150~
Line 151 This is line 151~
...
Line 160 This is line 160~
Line 161 This is line 161~
Line 162 This is line 162~
批量插入后,我的 table 行将如下所示:
Line 139 This is line 139~
Line 140 This is line 140~
...
Line 149 This is line 149~
Line 160 This is line 160~
Line 161 This is line 161~
Line 150 This is line 150~
Line 151 This is line 151~
Line 162 This is line 162~
第 160 行和第 161 行以某种方式在第 149 行和第 150 行之间得到 inserted/jumbled,这似乎发生在随机行号上,我已经测试过它是否是数据问题,但不是...有人有任何问题吗相关经验和解决方案?
它们没有被插入 "jumbled"。你是如何检索的?啊,就是这个问题。结果集(如表格)本质上是 无序的 。因此,如果您仅使用 select
查看数据,那么您可能会看到乱序。
最简单的解决方案是第一列是行号或其他内容。然后你可以这样做:
select abc.*
from abc
order by abc.linenumber;
一切都会正常的。
编辑:
如果需要添加行号,可以加载到视图中。像这样:
CREATE TABLE NDCArchitectBulkInsert (
NDCArchitectBulkInsertId int identity(1, 1) primary key,
. . . -- rest of the columns
);
CREATE VIEW v_NDCArchitectBulkInsert as
SELECT . . . -- rest of columns but not id
FROM NDCArchitectBulkInsert;
BULK INSERT v_NDCArchitectBulkInsert
FROM '\FTP\TestFiles\abc'
WITH (ROWTERMINATOR = '~');
我不是 100% 确定该行为是有保证的,但我认为在实践中这将按插入顺序更新身份。
我正在使用 SQL 服务器批量插入命令将带有终止符的 EDI 文件内容插入到我的 table 中,但是,有时顺序或行会出现混乱,例如:
BULK INSERT dbo.NDCArchitectBulkInsert
FROM '\FTP\TestFiles\abc'
WITH (ROWTERMINATOR = '~')
文件(abc)内容:
Line 139 This is line 139~
Line 140 This is line 140~
...
Line 149 This is line 149~
Line 150 This is line 150~
Line 151 This is line 151~
...
Line 160 This is line 160~
Line 161 This is line 161~
Line 162 This is line 162~
批量插入后,我的 table 行将如下所示:
Line 139 This is line 139~
Line 140 This is line 140~
...
Line 149 This is line 149~
Line 160 This is line 160~
Line 161 This is line 161~
Line 150 This is line 150~
Line 151 This is line 151~
Line 162 This is line 162~
第 160 行和第 161 行以某种方式在第 149 行和第 150 行之间得到 inserted/jumbled,这似乎发生在随机行号上,我已经测试过它是否是数据问题,但不是...有人有任何问题吗相关经验和解决方案?
它们没有被插入 "jumbled"。你是如何检索的?啊,就是这个问题。结果集(如表格)本质上是 无序的 。因此,如果您仅使用 select
查看数据,那么您可能会看到乱序。
最简单的解决方案是第一列是行号或其他内容。然后你可以这样做:
select abc.*
from abc
order by abc.linenumber;
一切都会正常的。
编辑:
如果需要添加行号,可以加载到视图中。像这样:
CREATE TABLE NDCArchitectBulkInsert (
NDCArchitectBulkInsertId int identity(1, 1) primary key,
. . . -- rest of the columns
);
CREATE VIEW v_NDCArchitectBulkInsert as
SELECT . . . -- rest of columns but not id
FROM NDCArchitectBulkInsert;
BULK INSERT v_NDCArchitectBulkInsert
FROM '\FTP\TestFiles\abc'
WITH (ROWTERMINATOR = '~');
我不是 100% 确定该行为是有保证的,但我认为在实践中这将按插入顺序更新身份。