SQL Server table is sorted by default

前端 未结 2 1731
遥遥无期
遥遥无期 2021-01-14 13:45

I have simple SSIS package where I import data from flat file into SQL Server table (SQL Server 005). File contains 70k rows and table has no primary key. Importing is suces

相关标签:
2条回答
  • 2021-01-14 13:56

    You cannot rely on ordering unless you specify order by in your SQL query. SQL is a relational algebra that works with sets. Those sets are unordered. Database tables do not have an intrinsic ordering.

    It may well be that the sets are ordered due to the way the data is retrieved from the tables. This may be based on primary key, order of insertion, clustered key, seemingly random order based on the execution plan of the query or the actual data in the table or even the phase of the moon.

    Bottom line, if you want a specific order, use order by. If you don't want a specific order, the DBMS is free to deliver your rows in any order, including one based on the first column.

    If you really want them sorted depending on the position in the import file, you should add another column to the table to store an increasing number based on its position in that file. Then use order by using that column. But that's a pretty arbitrary sort order, you're generally better off choosing one that makes more sense to the data (transaction ID, date/time, customer number or whatever else you have).

    If you want to avoid the default sort (however variable that may be), use a specific sort.

    0 讨论(0)
  • 2021-01-14 14:04

    In general no order is applied if there is no ordering in the select query.

    What I have noticed is that the table results might return in the order of the primary key, but this is not gaurenteed either.

    So all in all, if you do not specify a ordering, no ordering can be assumed.

    0 讨论(0)
提交回复
热议问题