Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retaining Original Sort Order

Status
Not open for further replies.

Erik70NY

MIS
Jun 19, 2001
5
US
I imported a text file into a table and discovered upon completion that the original order of the rows as they appear in the text file is not the same as they appear in the target table.

Is there a way to maintain the original order of the rows as they appear in the source file?

Thanks in advance.
 
No. By definition in SQL Server if you want the rows in a specific order you MUST specify that order in your select statement. You can make NO assumptions about what order the data is going to be stored in a table.
 
A clustered index will physically order the rows in a specific order, so if there was one on the table you imported the data into, that is the order it will put the rows in no matter what else you may do. As flutplr said, normally the row ordering is done in the select statement. A clustered index is often set up if there is a specific order that the records would normally need to be seen in.
 
Thanks for responses. Ultimately I managed to solve this problem by altering my export routine within my source database(multi-value pick flavor) to included a column with sequential numbering, and then just sorted on that within my target sql table.
 
SQL Sister, just to clarify, even with a clustered index there is no guarantee that a select without a sort clause will return the rows in the order of the clustered index.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top