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

クエリ(Unionの結果)は、orderbyステートメントなしで実行されています。これは、結果の順序が決定論的ではないことを意味します。つまり、実行ごとに異なる可能性があります。

これを修正するには、最後のwhereステートメントの後にOrderbyステートメントを追加する必要があります。

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

また、最後のクエリのyellowpage_nameステートメントによる順序は、最終結果セットの順序とは関係がないため、削除する必要があります。

于 2013-03-26T19:30:46.077 に答える
1

これはクエリの1つの地獄であるため、何が起こっているのかを正確に把握することは困難です。Order Byただし、完全な結果セットが欠落しています。

SQLServerはリレーショナルデータベースです。それがここでの重要な概念です。順序をデータベースエンジンにすることを明確に定義しない限り、返される行の順序については保証されません。2つの異なる場所でまったく同じクエリを使用して、結果の順序を変えることができます。

関係理論では、集合(A、B、C)は(C、B、A)と同じです。順序は関係ありません。

于 2013-03-26T19:31:26.957 に答える
0

さて、あなたはユニークではないorderkeyで注文します。同じオーダーキーを持つ行は、必ずしも同じ順序で表示されるとは限りません。

order by問題を解決するために句を拡張します。

于 2013-03-26T19:32:42.270 に答える