Unix Technical Forum

query question

This is a discussion on query question within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I have a question about a query which I want to share with you. Maybe someone knows how ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:15 AM
Rik Moed
 
Posts: n/a
Default query question

Hi,

I have a question about a query which I want to share with you. Maybe
someone knows how this strange behavior occurs.

Here is what I do:

SELECT top 1 actstage FROM v_OpportunityFase WHERE oppId = 247

This produces the result 4, which in my case is correct.

And now the following query:

SELECT ISNULL((SELECT top 1 actstage FROM v_OpportunityFase WHERE
oppId = 247), 7)

This query produces the result 5, this is very unexpected.

Does someone know why these two queries do not produce the same
result?

Thanks,
Rik Moed
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:15 AM
John Bell
 
Posts: n/a
Default Re: query question

Hi

TOP does not really make any sense unless you have the results ordered! SQL
Server does not guarantee order unless and ORDER BY clause is included. See
Books online: "ORDER BY clause, compatibility issues"


John
"Rik Moed" <rik.moed@capgemini.com> wrote in message
news:d57c2e75.0406160713.38f4390a@posting.google.c om...
> Hi,
>
> I have a question about a query which I want to share with you. Maybe
> someone knows how this strange behavior occurs.
>
> Here is what I do:
>
> SELECT top 1 actstage FROM v_OpportunityFase WHERE oppId = 247
>
> This produces the result 4, which in my case is correct.
>
> And now the following query:
>
> SELECT ISNULL((SELECT top 1 actstage FROM v_OpportunityFase WHERE
> oppId = 247), 7)
>
> This query produces the result 5, this is very unexpected.
>
> Does someone know why these two queries do not produce the same
> result?
>
> Thanks,
> Rik Moed



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:15 AM
Zach Wells
 
Posts: n/a
Default Re: query question

Queries do not returned ordered results unless you explicitly request it
by using an ORDER BY clause.

Zach

Rik Moed wrote:
> Hi,
>
> I have a question about a query which I want to share with you. Maybe
> someone knows how this strange behavior occurs.
>
> Here is what I do:
>
> SELECT top 1 actstage FROM v_OpportunityFase WHERE oppId = 247
>
> This produces the result 4, which in my case is correct.
>
> And now the following query:
>
> SELECT ISNULL((SELECT top 1 actstage FROM v_OpportunityFase WHERE
> oppId = 247), 7)
>
> This query produces the result 5, this is very unexpected.
>
> Does someone know why these two queries do not produce the same
> result?
>
> Thanks,
> Rik Moed

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 04:15 AM
Moed
 
Posts: n/a
Default Re: query question

> Queries do not returned ordered results unless you explicitly request it
> by using an ORDER BY clause.




Ok,
But the results are ordered in the view v_OpportunityFase.

Rik

>
> Zach
>
> Rik Moed wrote:
> > Hi,
> >
> > I have a question about a query which I want to share with you. Maybe
> > someone knows how this strange behavior occurs.
> >
> > Here is what I do:
> >
> > SELECT top 1 actstage FROM v_OpportunityFase WHERE oppId = 247
> >
> > This produces the result 4, which in my case is correct.
> >
> > And now the following query:
> >
> > SELECT ISNULL((SELECT top 1 actstage FROM v_OpportunityFase WHERE
> > oppId = 247), 7)
> >
> > This query produces the result 5, this is very unexpected.
> >
> > Does someone know why these two queries do not produce the same
> > result?
> >
> > Thanks,
> > Rik Moed



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 04:16 AM
Erland Sommarskog
 
Posts: n/a
Default Re: query question

Moed (rikmoed@wanadoo.nl) writes:
> But the results are ordered in the view v_OpportunityFase.


They are not. The view may include an ORDER BY clause, and the view
may even be an indexed and thus materialized view. But by definition,
data is not stored in order in a relational database, but data is
stored as unordered sets. Physically there may be an order, but
conceptually there is not. The only way to get data in a certain
order from the database, is to use an ORDER BY clause.
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:59 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com