我有一个存储过程,在调用时返回以下值:
EXEC @return_value = [dbo].[Activities_GetCrazyTables]
@iActivity = 49,
@iLanding = 6,
@iFirst = 1,
@iLast = 10,
@strSearch = '',
@bActiveOnly = 0
orderkey yellowpage_rownum yellowpage_id yellowpage_name
表4 1 2022
牛山
table4 2 2055 Lake Mendocino
表4 3 2099 吊灯
但是,当我在查询分析器中执行存储过程的实际代码时,它会以不同的方式返回结果:
orderkey yellowpage_rownum yellowpage_id yellowpage_name
表4 1 2022
吊灯
table4 2 2055 牛山
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
答案 0 :(得分:1)
您的查询(联盟的结果)在没有order by语句的情况下运行。这意味着结果的顺序不是确定性的,这意味着它在不同的运行中可能是不同的。
要解决此问题,您需要在最后一个where语句后添加一个Order by Statement:
as y where y.yellowpage_rownum between @iFirst and @iLast Order by col1, col2
您还应该通过最后一个查询中的yellowpage_name语句消除该顺序,因为这与最终结果集的排序无关。
答案 1 :(得分:1)
这是一个查询的地狱,因此很难准确地了解正在发生的事情。但是,您在完整结果集上缺少Order By
。
SQL Server是关系数据库。这是关键概念。除非您明确定义了所需的顺序,否则数据库引擎无法保证返回的行的顺序。您可以在两个不同的位置使用完全相同的查询,并获得不同的结果顺序。
在关系理论中,集合(A,B,C)与(C,B,A)相同。订单无关紧要。
答案 2 :(得分:0)
好吧,你按orderkey订购,这不是唯一的。具有相同orderkey的行不一定以相同的顺序出现。
扩展您的order by
子句以解决问题。