[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

sali

3/20/2007 7:30:00 AM

sql 2000
having table tab1 copied into table tab2 with ordered select, like

select *
into tab2
from tab1
order by key1

is there some guarrantie that tab2 will then also appear ordered in key1
order in subsequent raw selects, like

select *
from tab2

is there some physical memory or buffer or page limit which may guard
resulted tab2 to appear/not appear as ordered?

thnx


7 Answers

Tibor Karaszi

3/20/2007 7:55:00 AM

0

There is never ever any guaranteed order of the rows unless you specify ORDER BY in your SELECT
statement.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/d...
http://www.solidqualitylea...


"sali" <sali@euroherc.hr> wrote in message news:uv$WCGsaHHA.4788@TK2MSFTNGP04.phx.gbl...
> sql 2000
> having table tab1 copied into table tab2 with ordered select, like
>
> select *
> into tab2
> from tab1
> order by key1
>
> is there some guarrantie that tab2 will then also appear ordered in key1 order in subsequent raw
> selects, like
>
> select *
> from tab2
>
> is there some physical memory or buffer or page limit which may guard resulted tab2 to appear/not
> appear as ordered?
>
> thnx
>
>


MC

3/20/2007 7:55:00 AM

0

Select as such is not ordered and depending on exec plan you may get
unordered results. Specify order by in SELECT statement if you wish data to
bi ordered. You could assume that the data will be retrieved using clustered
index scan and in that case data should be ordered on the clustered index
keys. Thats assuming offcourse, and I dont like to assume too much :).

MC

"sali" <sali@euroherc.hr> wrote in message
news:uv$WCGsaHHA.4788@TK2MSFTNGP04.phx.gbl...
> sql 2000
> having table tab1 copied into table tab2 with ordered select, like
>
> select *
> into tab2
> from tab1
> order by key1
>
> is there some guarrantie that tab2 will then also appear ordered in key1
> order in subsequent raw selects, like
>
> select *
> from tab2
>
> is there some physical memory or buffer or page limit which may guard
> resulted tab2 to appear/not appear as ordered?
>
> thnx
>
>


xyb

3/20/2007 8:06:00 AM

0

On 3?20?, ??3?54?, "MC" <marko.culoNOS...@gmail.com> wrote:
> Select as such is not ordered and depending on exec plan you may get
> unordered results. Specify order by in SELECT statement if you wish data to
> bi ordered. You could assume that the data will be retrieved using clustered
> index scan and in that case data should be ordered on the clustered index
> keys. Thats assuming offcourse, and I dont like to assume too much :).
>
> MC
>
> "sali" <s...@euroherc.hr> wrote in message
>
> news:uv$WCGsaHHA.4788@TK2MSFTNGP04.phx.gbl...
>
>
>
> > sql 2000
> > having table tab1 copied into table tab2 with ordered select, like
>
> > select *
> > into tab2
> > from tab1
> > order by key1
>
> > is there some guarrantie that tab2 will then also appear ordered in key1
> > order in subsequent raw selects, like
>
> > select *
> > from tab2
>
> > is there some physical memory or buffer or page limit which may guard
> > resulted tab2 to appear/not appear as ordered?
>
> > thnx- ??????? -
>
> - ??????? -

I test some code here only to find that if a table have no index and
your order by will be valid.

drop table #t
create table #t
(
c1 int,
c2 char(1)
)

insert into #t
select 1,'a'
union select 2,'b'
union select 3,'c'
union select 1,'b'
union select 1,'c'

select * from #t

select * into #tt from #t order by c1

select * from #tt

drop table #tt

select * into #tt from #t order by c2

select * from #tt

drop table #tt

drop table #t

David Portas

3/20/2007 8:08:00 AM

0

On 20 Mar, 07:54, "MC" <marko.culoNOS...@gmail.com> wrote:
> You could assume that the data will be retrieved using clustered
> index scan and in that case data should be ordered on the clustered index
> keys.

Just to be clear, that's actually two assumptions - both can be very
wrong. The query may or may not perform a clustered scan. Even if a
clustered scan is used the data won't necessarily be retrieved in
cluster key order - something that has been extensively discussed here
in the past.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/librar...(en-US,SQL.90).aspx
--

David Portas

3/20/2007 8:10:00 AM

0

On 20 Mar, 07:30, "sali" <s...@euroherc.hr> wrote:
> sql 2000
> having table tab1 copied into table tab2 with ordered select, like
>
> select *
> into tab2
> from tab1
> order by key1
>
> is there some guarrantie that tab2 will then also appear ordered in key1
> order in subsequent raw selects, like
>
> select *
> from tab2
>
> is there some physical memory or buffer or page limit which may guard
> resulted tab2 to appear/not appear as ordered?
>
> thnx

Tables are unordered sets by definition. There is no "physical order"
of a table.

The order of query results is undefined unless you use ORDER BY. Even
if you appear to get an ordered result when you don't use ORDER BY
there is no guarantee that the result will be repeatable.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/librar...(en-US,SQL.90).aspx
--

David Portas

3/20/2007 8:13:00 AM

0

On 20 Mar, 08:05, "xyb" <xiangyua...@gmail.com> wrote:
>
> I test some code here only to find that if a table have no index and
> your order by will be valid.
>
> drop table #t
> create table #t
> (
> c1 int,
> c2 char(1)
> )
>
> insert into #t
> select 1,'a'
> union select 2,'b'
> union select 3,'c'
> union select 1,'b'
> union select 1,'c'
>
> select * from #t
>
> select * into #tt from #t order by c1
>
> select * from #tt
>
> drop table #tt
>
> select * into #tt from #t order by c2
>
> select * from #tt
>
> drop table #tt
>
> drop table #t- Hide quoted text -
>

No. The result is undefined, something made quite clear in the
documentation. The fact that you get some particular ordering once,
twice or even a hundred times doesn't prove that it will always be so.
For dependable results please read the documentation and go by logic
rather than trust the whim of the Query Optimizer.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/librar...(en-US,SQL.90).aspx
--



sali

3/20/2007 8:55:00 AM

0

"David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> je napisao u poruci
interesnoj grupi:1174378366.312245.26910@y80g2000hsf.googlegroups.com...
> On 20 Mar, 08:05, "xyb" <xiangyua...@gmail.com> wrote:
>>
>> I test some code here only to find that if a table have no index and
>> your order by will be valid.
>>

>
> No. The result is undefined, something made quite clear in the
> documentation. The fact that you get some particular ordering once,
> twice or even a hundred times doesn't prove that it will always be so.


thnx, that is the answer i was fear off.

we have one quite complex query [whole sp in fact], containinig piece of
code depoending on ordered select, as described in my post.

problem is that it realy *sometimes* fails, but mostly gives reasonable
results.

this may be the explanation.

thnx for clarification.