我有一个存储过程,它在调用时返回以下值:
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