Inserting lots of data into a temp table, does not keep sort order

  • Not sure if this will help, but can you verify the table collation? Perhaps the default collation is different from the previous installation? That would treat lower/upper case and other things differently...

    Hope that helps.

    Mike

  • Rhonda,

    Review what Frank and Jonathan said initially. The fact is, there is no way to guarantee the results that you want without using an ORDER BY on the final SELECT.  For a small amount of data, your method of ordering the data as it's inserted will probably work. But again, that's just a coincidence. All of those intermediate order by's are meaningless. You must use ORDER BY when selecting from the final results table.

    Mike

     

  • I was having the same problem many months ago - I was trying to insert an identity column in the first temp. table and it still wouldn't maintain the order.  I found a KB article that said there was a known bug regarding identity columns and order by clause (but I can't find the article now).  This is the workaround that was offered and I'm using it and it works just fine.  When you insert into the second table from the first, add an identity column. 

    select IDentity(int,1,1) as Idnum, * into #temp2 from #temp1 order by lastname, firstname

    Now your second table will be in the correct order.

     

     

Viewing 3 posts - 16 through 17 (of 17 total)

You must be logged in to reply to this topic. Login to reply