0

我有一个存储过程,它在调用时返回以下值:

EXEC    @return_value = [dbo].[Activities_GetCrazyTables]
        @iActivity = 49,
        @iLanding = 6,
        @iFirst = 1,
        @iLast = 10,
        @strSearch = '',
        @bActiveOnly = 0

orderkeyyellowpage_rownumyellowpage_idyellowpage_name
table4      1      2022       Cow Mountain
table4      2      2055      Lake Mendocino
table4      3      2099      Chandelier

但是,当我在查询分析器中执行存储过程的实际代码时,它返回的结果不同:

orderkeyyellowpage_rownumyellowpage_idyellowpage_name
table4      1      2022       Chandelier
table4      2      2055      Cow Mountain
table4      3      2099      Lake Mendocino

与执行存储过程相比,为什么单独运行查询时获得正确的排序顺序?

存储过程的代码如下:

set @strSearch = LOWER(@strSearch)

select orderkey, y.* from 
(
    /* get the featured sponsors in order specified in the database */
    select ROW_NUMBER() over (order by x.orderkey) as yellowpage_rownum, x.* 
    from (
    /* top tier featured 1-5 */
    select yellowpage_id, yellowpage_name, yellowpage_address1, yellowpage_address2, yellowpage_city, yellowpage_state, yellowpage_zip, 
    yellowpage_phone, yellowpage_url, yellowpage_latitude, yellowpage_longitude, yellowpage_featuredsponsor, yellowpage_premium, yellowpage_order, 
    yellowpageactivity_order, story_count,
    'table1' as orderkey, yellowpage_active from
    (select top 5 a.* from 
    (select ROW_NUMBER() over (order by yellowpage_name) as rownum, yellowpage_id, yellowpage_name, yellowpage_address1, yellowpage_address2, yellowpage_city, yellowpage_state, yellowpage_zip, 
    yellowpage_phone, yellowpage_url, yellowpage_latitude, yellowpage_longitude, yellowpage_featuredsponsor, yellowpage_premium, yellowpage_order, 
    yellowpageactivity_order, (select COUNT(storyyellowpage_story) from storyyellowpagexref join stories on (story_id=storyyellowpage_story) where story_active=1 and storyyellowpage_yellowpage=y.yellowpage_id) as story_count, yellowpage_active
    from yellowpageactivityxref 
    join activities on (activity_id=yellowpageactivity_activity) 
    join yellowpages y on (yellowpage_id=yellowpageactivity_yellowpage) 
    join landingcities on (landingcity_city=yellowpage_city) 
    where yellowpageactivity_activity=@iActivity and landingcity_landing=@iLanding and yellowpage_featuredsponsor=1 and 
        (LOWER(yellowpage_name) LIKE '%' + @strSearch + '%' or LOWER(yellowpage_address1) LIKE '%' + @strSearch + '%')) as a 
    where a.rownum between 1 and 5 order by newid()) as b
    union all
    /* top tier featured 6-10 */
    select yellowpage_id, yellowpage_name, yellowpage_address1, yellowpage_address2, yellowpage_city, yellowpage_state, yellowpage_zip, 
    yellowpage_phone, yellowpage_url, yellowpage_latitude, yellowpage_longitude, yellowpage_featuredsponsor, yellowpage_premium, yellowpage_order, 
    yellowpageactivity_order, story_count,
    'table2' as orderkey, yellowpage_active from
    (select top 5 a.* from 
    (select ROW_NUMBER() over (order by yellowpage_name) as rownum, yellowpage_id, yellowpage_name, yellowpage_address1, yellowpage_address2, yellowpage_city, yellowpage_state, yellowpage_zip, 
    yellowpage_phone, yellowpage_url, yellowpage_latitude, yellowpage_longitude, yellowpage_featuredsponsor, yellowpage_premium, yellowpage_order, 
    yellowpageactivity_order, (select COUNT(storyyellowpage_story) from storyyellowpagexref join stories on (story_id=storyyellowpage_story) where story_active=1 and storyyellowpage_yellowpage=y.yellowpage_id) as story_count, yellowpage_active
    from yellowpageactivityxref 
    join activities on (activity_id=yellowpageactivity_activity) 
    join yellowpages y on (yellowpage_id=yellowpageactivity_yellowpage) 
    join landingcities on (landingcity_city=yellowpage_city) 
    where yellowpageactivity_activity=@iActivity and landingcity_landing=@iLanding and yellowpage_featuredsponsor=1 and 
        (LOWER(yellowpage_name) LIKE '%' + @strSearch + '%' or LOWER(yellowpage_address1) LIKE '%' + @strSearch + '%')) as a 
    where a.rownum between 6 and 10 order by newid()) as b
    union all
    /* top tier featured 11-15 */
    select yellowpage_id, yellowpage_name, yellowpage_address1, yellowpage_address2, yellowpage_city, yellowpage_state, yellowpage_zip, 
    yellowpage_phone, yellowpage_url, yellowpage_latitude, yellowpage_longitude, yellowpage_featuredsponsor, yellowpage_premium, yellowpage_order, 
    yellowpageactivity_order, story_count,
    'table3' as orderkey, yellowpage_active from
    (select top 5 a.* from 
    (select ROW_NUMBER() over (order by yellowpage_name) as rownum, yellowpage_id, yellowpage_name, yellowpage_address1, yellowpage_address2, yellowpage_city, yellowpage_state, yellowpage_zip, 
    yellowpage_phone, yellowpage_url, yellowpage_latitude, yellowpage_longitude, yellowpage_featuredsponsor, yellowpage_premium, yellowpage_order, 
    yellowpageactivity_order, (select COUNT(storyyellowpage_story) from storyyellowpagexref join stories on (story_id=storyyellowpage_story) where story_active=1 and storyyellowpage_yellowpage=y.yellowpage_id) as story_count, yellowpage_active
    from yellowpageactivityxref 
    join activities on (activity_id=yellowpageactivity_activity) 
    join yellowpages y on (yellowpage_id=yellowpageactivity_yellowpage) 
    join landingcities on (landingcity_city=yellowpage_city) 
    where yellowpageactivity_activity=@iActivity and landingcity_landing=@iLanding and yellowpage_featuredsponsor=1 and 
        (LOWER(yellowpage_name) LIKE '%' + @strSearch + '%' or LOWER(yellowpage_address1) LIKE '%' + @strSearch + '%')) as a 
    where a.rownum between 11 and 15 order by newid()) as b
    union all
    /* get the rest and sort by name */
    select yellowpage_id, yellowpage_name, yellowpage_address1, yellowpage_address2, yellowpage_city, yellowpage_state, yellowpage_zip, 
    yellowpage_phone, yellowpage_url, yellowpage_latitude, yellowpage_longitude, yellowpage_featuredsponsor, yellowpage_premium, yellowpage_order, 
    yellowpageactivity_order, story_count,
    'table4' as orderkey, yellowpage_active from
    (select top 2147483647 a.* from 
    (select yellowpage_id, yellowpage_name, yellowpage_address1, yellowpage_address2, yellowpage_city, yellowpage_state, yellowpage_zip, 
    yellowpage_phone, yellowpage_url, yellowpage_latitude, yellowpage_longitude, yellowpage_featuredsponsor, yellowpage_premium, yellowpage_order, 
    yellowpageactivity_order, (select COUNT(storyyellowpage_story) from storyyellowpagexref join stories on (story_id=storyyellowpage_story) where story_active=1 and storyyellowpage_yellowpage=y.yellowpage_id) as story_count, yellowpage_active
    from yellowpageactivityxref 
    join activities on (activity_id=yellowpageactivity_activity) 
    join yellowpages y on (yellowpage_id=yellowpageactivity_yellowpage) 
    join landingcities on (landingcity_city=yellowpage_city) 
    where yellowpageactivity_activity=@iActivity and landingcity_landing=@iLanding and yellowpage_featuredsponsor=0 and 
        (LOWER(yellowpage_name) LIKE '%' + @strSearch + '%' or LOWER(yellowpage_address1) LIKE '%' + @strSearch + '%')) as a 
    order by yellowpage_name) as b
    ) as x
    ) as y where y.yellowpage_rownum between @iFirst and @iLast
4

3 回答 3

1

您的查询(联合的结果)在没有 order by 语句的情况下运行。这意味着结果的顺序不是确定性的,这意味着它在不同的运行中可能会有所不同。

要解决此问题,您需要在最后一个 where 语句之后添加 Order by Statement:

as y where y.yellowpage_rownum between @iFirst and @iLast Order by col1, col2

您还应该消除最后一个查询中的黄色页面名称语句的顺序,因为这与最终结果集的顺序无关。

于 2013-03-26T19:30:46.077 回答
1

这是一个地狱般的查询,因此很难准确理解正在发生的事情。但是,您Order By在完整的结果集上缺少一个。

SQL Server 是一个关系数据库。这是这里的关键概念。除非您明确定义您希望的顺序是什么,否则数据库引擎不保证返回的行的顺序。您可以在两个不同的地方使用完全相同的查询并获得不同的结果顺序。

在关系理论中,集合 (A, B, C) 与 (C, B, A) 相同。顺序不相关。

于 2013-03-26T19:31:26.957 回答
0

好吧,您通过 orderkey 订购,这不是唯一的。具有相同 orderkey 的行不一定以相同的顺序出现。

扩展您的order by条款以解决问题。

于 2013-03-26T19:32:42.270 回答