简体   繁体   English

在批量插入中保留行顺序

[英]Preserve row order in bulk insert

I'm trying to bulk insert a table from a text file and preserve the row order.我正在尝试从文本文件中批量插入表格并保留行顺序。 I'm doing this for two reasons:我这样做有两个原因:

  • I need to check if the first row is a header, and remove it if so我需要检查第一行是否是 header,如果是则将其删除
  • I am reporting back "errors"(differences between what a column value should be and what was given ie a letter in an alphanumeric field), and need to tell them which row in the text file they occurred at.我正在报告“错误”(列值应该是什么和给出什么之间的差异,即字母数字字段中的字母),并且需要告诉他们它们出现在文本文件中的哪一行。 At the same time I need to still insert these rows, just with blank values on the columns that have errors.同时我仍然需要插入这些行,只是在有错误的列上使用空白值。

To do the bulk insert I am first inserting it into a temp table, and then inserting the cleaned up values into the main table.为了进行批量插入,我首先将其插入到临时表中,然后将清理后的值插入到主表中。 But the temp table seems to be in a random order.但临时表似乎是随机排列的。

The input file is not in any particular order, so I can't solve this with an order-by query.输入文件没有任何特定的顺序,所以我无法通过 order-by 查询来解决这个问题。

Any suggestions on what I can do here?关于我在这里可以做什么的任何建议?

For the table or temp table you are inserting to, just have a column with the IDENTITY PROPERTY对于您要插入的表或临时表,只需有一列带有IDENTITY PROPERTY

create table #temp (ID int identity(1,1), otherColumn varchar(16), ...)
--do bulk insert

But, what may be more ideal for you is to create a unique row number in the source file.但是,对您来说更理想的是在源文件中创建一个唯一的行号。

 Using _con As New SqlConnection(ConnectionString)
  _con.Open()
  If Not (String.IsNullOrEmpty(Pkey)) Then
    deleteTbl = "Delete from " & destTableName & " WHERE " & Pkey & " = " & PkeyId
    Using cmdTable As New SqlCommand(deleteTbl, _con)
      cmdTable.ExecuteNonQuery()
    End Using
  End If


  Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(_con)
    bulkCopy.BulkCopyTimeout = 0
    bulkCopy.BatchSize = 50000
    bulkCopy.DestinationTableName = destTableName 'it is a tablename
    bulkCopy.WriteToServer(sourceTable)  'sourceTable is a datatable
  End Using

End Using

Create a table with identity column, then bulk insert into a view on top of that table, as follows:创建一个带有标识列的表,然后批量插入到该表顶部的视图中,如下所示:

CREATE TABLE tb_TextFile (ID int identity(1,1), someColumn varchar(16))
GO

CREATE VIEW vw_TextFile
AS
SELECT someColumn
FROM tb_TextFile
GO

BULK INSERT vw_TextFile
FROM ...

Keying off of Christophej's suggestion with a few other additions:通过其他一些补充来结束 Christophej 的建议:

CREATE TABLE dbo.twrkImportFileLine ( ID INT IDENTITY(1,1) PRIMARY KEY, LineData NVARCHAR(MAX) ); 

GO

CREATE OR ALTER VIEW dbo.vw_twrkImportFileLine AS
SELECT LineData
FROM twrkImportFileLine;

GO

--INSERT INTO vw_twrkImportFileLine (LineData) VALUES ('Test inserting text into the table through the view'); 
--SELECT * FROM vw_twrkImportFileLine
--DELETE FROM vw_twrkImportFileLine

BULK INSERT dbo.vw_twrkImportFileLine FROM '\\SERVERNAME\Import Folder\FileName.txt' WITH (TABLOCK) ;

SELECT LineData
FROM dbo.twrkImportFileLine 
ORDER BY ID;

The order was maintained in my 815 line sample file that I tested.该订单在我测试的 815 行示例文件中维护。 It was previously out of order without this method in large sections most likely due to parallelism.如果没有这种方法,很可能是由于并行性,在大部分情况下,它以前是乱序的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM