• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 900
  • Last Modified:

Bulk Insert and Record Order

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
MarkMahon
Asked:
MarkMahon
  • 4
  • 4
  • 2
  • +2
3 Solutions
 
JestersGrindCommented:
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
 
Richard QuadlingSenior Software DeveloperCommented:
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
 
venk_rCommented:
If the table contains a clustered index ,yes it will put them in the same order.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jorgedeoliveiraborgesCommented:
>> Will the records in the SQL table be in the same order as that of the text table?
Yes.
0
 
MarkMahonAuthor Commented:
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
 
Richard QuadlingSenior Software DeveloperCommented:
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
 
venk_rCommented:
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
 
Richard QuadlingSenior Software DeveloperCommented:
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
 
MarkMahonAuthor Commented:
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
 
Richard QuadlingSenior Software DeveloperCommented:
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
 
MarkMahonAuthor Commented:
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
 
MarkMahonAuthor Commented:
Nice job guys and gals
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now