0

質問するのが苦手なので、欲しいものの例を挙げます。

if i = 1 and xi = 0 then
    select a,b,c,d,e,f,g where z = 1
elseif i=0 and xi = 1 then
    select a,c,f,h,l,n where w = var
elseif i=1 and xi=1 then
    select a,b,c,d,e,f,g, where z = 1
    union all
    select a,c,f,h,l,n where w = var
end if

列が等しくなく、両方に一意の条件がある場合、2 つの select ステートメントを結合するにはどうすればよいですか?

4

2 に答える 2

0

動的 SQL を使用しないことが要件でない場合は、動的 SQL を選択します。

もう 1 つのアイデアは、ユーザー定義関数 returnin テーブルを使用することです。

そこで、ロジックをカプセル化します...

于 2013-10-04T09:44:34.203 に答える
0

条件に基づいて、派生テーブルを作成して目的の列を取得し、2 つのテーブルの結合を取得して、列数が少ない派生テーブルの列リストに null 値を追加できます。

擬似コード:

select * from 
(select a,b,c,d,e,f,g
 where  z = 1 
 and 1 = case when i = 1 and xi = 0 then 1 
             when i = 1 and xi = 1 then 1
             else 0 
        end) as T1
 union all             
(select a,c,f,h,l,n ,null -- add null value to equate number of columns
where w = var 
and 1 = case when i=0 and xi = 1 then 1 
             when i=1 and xi = 1 then 1
             else 0 
        end) as T2

お役に立てれば!!!

于 2013-10-04T09:35:50.947 に答える