question

michelle avatar image
michelle asked

how to get results in ascending order when creating table

I am trying to create a temp table and my query reads as

Create table temp.Customer_All as (select ID, Name, ... from Customer... order by ID) with data;

However, order by is not allowed in subquery. Does anyone know how to obtain an answer set with ID in ascending order when creating a table?

temporary-table
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Abhinav avatar image
Abhinav answered

use @@identity for automatic row id increase

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Magnus Ahlkvist avatar image
Magnus Ahlkvist answered

It's a misconception that the rows in a table are ordered.

It's when you query a table that you can specify in which order you want the rows. You can have a Clustered index on a table, which in a way will actually order the datarows. But still, if you query the table without an ORDER BY clause, you'll get the rows in whatever order SQL Server finds most efficient.

So just go ahead creating the temp table, add an index to the ID column in the temp table and use ORDER BY ID when you query your temp table.

Regards ///Magnus

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
+1: misconception is usually held when clustered index is created too - bad myth, very bad myth!
4 Likes 4 ·
Magnus Ahlkvist avatar image Magnus Ahlkvist commented ·
There are times when I'd like to be able to control the order of the inserts, but that's for avoiding fragmenting data when I have a natural key like (Surname,Christianname) as clustered index.
0 Likes 0 ·
Oleg avatar image
Oleg answered

I am not used to the syntax in the question, because it smells like Oracle. With standard T-SQL the ordering can be achieved by using select into from syntax, so if the query in question is restated as

select * into temp.Customer_All
    from Customer
    order by ID;

then the rows will be inserted in the ID order. This does not guarantee though that they will be stored in the same order simply because the temp.Customer_All will end up being a heap table. However, assuming that the syntax in the question is valid in SQL Server 2008, it is easily possible to achieve the same effect (control the order in which the rows are inserted) by using a technique commonly referred to as a cheesy SQL (to allow the order by inside of the sub-select):

create table temp.Customer_All as 
(
    select top 100 percent
        ID, Name, ... 
        from Customer... 
        order by ID
) with data; 

Again, this will control the order in which the records are inserted which has nothing to do with how they end up actually stored as the temp.Customer_All is still going to be a heap table. On the other hand, altering the table by adding a clustered index on ID column will address the issue with logical ordering of the stored records, and this means that none of the nonsence with attempting to control the order in which the records are inserted is actually necessary.

Oleg

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David 1 avatar image David 1 commented ·
Oleg, the CREATE TABLE AS syntax is not just Oracle, it's standard SQL. Unfortunately SQL Server does not support it - not even 2008. It would be a nice thing to have though :)
2 Likes 2 ·
TimothyAWiseman avatar image TimothyAWiseman commented ·
Oleg, I have tested it many times and with a newly created heap table (one that has undergone only inserts not updates or deletes), selecting from that table will *normally* store and return the data in the order inserted when you do a select without an order by. With all that said, this is not gaurunteed. There are ways it can wind up out of order and no one should ever count on it being in that order. It normally works out that way, but not always.
0 Likes 0 ·
Oleg avatar image Oleg commented ·
@Timothy - Yes, but isn't it this is what I tried to point out, i.e. controlling the order in whcih the data is inserted does not guarantee the order in which the data is stored, and therefore, there is not a need to even try doing it? Sorry if I was not able to get my point across clearly. If such control is nevertheless desired then the cheesy SQL select top 100 percent ...order by... in the sub-select does work.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.