Solved

Bulk Insert and Record Order

Posted on 2011-09-06
12
734 Views
Last Modified: 2012-06-27
We are reading a text file into a SQL table using Bulk Insert.  Will the records in the SQL table be in the same order as that of the text table?

Thanks,
Mark
0
Comment
Question by:MarkMahon
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 21

Accepted Solution

by:
JestersGrind earned 167 total points
ID: 36488516
It depends.  Is there a clustered index on the table that you are importing into?  That will determine the order.  If there isn't a clustered index, the order will be fairly random.  Why is the order of the table important?  When you read the data, you can order it with the ORDER BY clause.

Greg

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 36488517
Normally, the row order is irrelevant.

But, if you are importing a table which is a parent table and you have other tables linking to this one, then you need to preserve the rows primary key.

I use code like this
 TRUNCATE TABLE TripleCDataConversion.dbo.Addresses
 BULK INSERT TripleCDataConversion.dbo.Addresses
 FROM 'D:\CCC2SQL\AD.TAB'
 WITH
  (
  FIELDTERMINATOR = '\t',
  KEEPIDENTITY,
  KEEPNULLS,
  LASTROW =               3313,
  ORDER (UniqueID ASC)
  )

Open in new window


The data has an identity column and I want to keep it when it reaches the SQL table.

Other than that, the order is irrelevant in my experience.
0
 
LVL 8

Assisted Solution

by:venk_r
venk_r earned 167 total points
ID: 36488519
If the table contains a clustered index ,yes it will put them in the same order.
0
 
LVL 6

Expert Comment

by:jorgedeoliveiraborges
ID: 36488571
>> Will the records in the SQL table be in the same order as that of the text table?
Yes.
0
 

Author Comment

by:MarkMahon
ID: 36488606
Jordedeoliveiraborges and Others -

I seem to be reading two answers that are at the opposite end of the spectrum ...

One (Jordedeoliveiraborges) says Yes but others say probably not as they will be stored in a random order.

Whos is correct?

Thanks,
Mark
0
 
LVL 40

Assisted Solution

by:RQuadling
RQuadling earned 166 total points
ID: 36488623
Depends.

If the table has a clustered index, then the data will be held in that order.

If the data in the text file is in the same order, then the data will be stored that way.

So, assuming 1 column of names and that the data is clustered on that column, the data will be held as

Adam
Brian
Charlie.

If the text file has the data as

Charlie
Brian
Adam

then the order will NOT be preserved.

So. The answer is ... it depends.

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:venk_r
ID: 36488634
Look at the link
http://msdn.microsoft.com/en-us/library/ms188365.aspx

ORDER ( { column [ ASC | DESC ] } [ ,... n ] )


Specifies how the data in the data file is sorted. Bulk import performance is improved if the data being imported is sorted according to the clustered index on the table, if any. If the data file is sorted in a different order, that is other than the order of a clustered index key or if there is no clustered index on the table, the ORDER clause is ignored. The column names supplied must be valid column names in the destination table. By default, the bulk insert operation assumes the data file is unordered. For optimized bulk import, SQL Server also validates that the imported data is sorted.

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 36488640
In my experience, as soon as someone mentions "held in order" with regard to SQL, then something is missing from the question.

Specifically, why is the order important?

0
 

Author Comment

by:MarkMahon
ID: 36488948
I should've mentioed that there is no Clustered Index (yet).

The order is important in the application and because it is we may have to add an id column in the text file to maintain the order.  

But before we do that I am just trying to understand what the "default" behavior of SQL for this situation.  Given a text file being imported into SQL via Bulk Insert, and no clustered index is specified, will the order on the sql table be the same as the text file.  It sounds like the answer is NO.

Thanks,
Mark
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 36488976
There is no "order" unless the data is clustered and/or indexed.

Add the order column to the data in the text file.

Use that column as a clustered index and primary key.

This will be the simplest way to preserve the order.
0
 

Author Comment

by:MarkMahon
ID: 36489112
Thank you to all very well done.

I actually "looked" ... opened the text files in Excel and compared to the SQL table resulting from the Bulk Insert... and yep the two files are NOT in the same order.

Thank you much, Mark
0
 

Author Closing Comment

by:MarkMahon
ID: 36489136
Nice job guys and gals
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now