呼び出されたときに以下の値を返すストアドプロシージャがあります。
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