Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 SELECT...INTO with ORDER BY

Author  Topic 

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-09-17 : 10:06:46
Is there a way to use SELECT...INTO with an ORDER BY clause and ensure the ordering is done before the INTO?

I have a SELECT...INTO with an IDENTITY(int,1,1) as RowNum. It seems that the ordering beging done after the temp table is populated and the the order of the IDENTITY is based on the clustered index used rather than the ORDER BY clasue.

Consider ...

--create intotest with clustered index on a
create table intotest (
a int identity(1,1) not null primary key,
b int not null)

--populate with random numbers
insert into intotest (b)
select n from numbers order by newid()
go

--create temp table ordered by b
select
identity(int,-1,-1) as RowNum,
b
into
#temp
from
intotest
order by
b

--what happened to my ordering?
select top 10 * from #temp order by rownum
go

--try again
drop table intotest
drop table #temp
go

--create intotest, this time with the clustered index on b
create table intotest (
a int identity(1,1) not null,
b int not null,
constraint pk_intotest primary key nonclustered (a))
create clustered index cx_intotest on intotest(b)

insert into intotest (b)
select n from numbers order by newid()
go

select
identity(int,-1,-1) as RowNum,
b
into
#temp
from
intotest
order by
b

--now that's right ...
select top 10 * from #temp order by rownum
go

 
Any ideas how I could rewrite the first select...into so the ordering is correct?

Jay White
{0}

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-09-17 : 10:12:09
I know this is a smartass answer, but....
quote:
Any ideas how I could rewrite the first select...into so the ordering is correct?
Uh, both SELECT...INTO's are the same. Adding the clustered index is what makes it order the way you want. And what's wrong with doing it the way it works now? Is the performance horrendous?

Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-09-17 : 10:17:19
Yeah the clustered index on the table in question must be on the pk to support many many queries, but I want to order by temp table by a different column (its a paging thing) ...

I could just create #temp explicitly and then INSERT ... SELECT ... ORDER BY, but I hate it when SQL tells me I can't do something one way or another ... ... I suppose my question is really more academic than anything else ...

Jay White
{0}
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-09-17 : 10:21:42
answered my own question ....

--create temp table ordered by b
select
identity(int,-1,-1) as RowNum,
b
into
#temp
from
(select top 100 percent
b
from
intotest
order by
b) a

 
...forgot about that 100 percent trick to get an order by in a derived table ...

Jay White
{0}
Go to Top of Page

JERICHO
Starting Member

18 Posts

Posted - 2002-09-17 : 23:43:43
there is error in the second select statement where is shows syntax error

Go to Top of Page
   

- Advertisement -