2

テーブルから行が返されない場合は、ダミー値を取得する必要があります。存在する場合はそれ自体で機能しますが、ユニオンでエラーが発生します。誰かが解決策または回避策を教えてもらえますか?

create table test1 (col1 varchar(10))    
create table test2 (col1 varchar(10))    
create table test3 (col1 varchar(10))


insert test1 values ('test1-row1')    
insert test1 values ('test1-row2')    
insert test2 values ('test2-row1')    
insert test2 values ('test2-row2')

select col1 from test1    
union    
select col1 from test2    
union    
if exists (select * from test3)    
    select col1 from test3    
else    
    select 'dummy'
4

2 に答える 2

9

test3が空の場合にダミー行を返す別のユニオンを追加できます。

select col1 from test1    
union    
select col1 from test2    
union    
select col1 from test3    
union
select 'dummy' where not exists (select * from test3)
于 2010-04-05T18:22:58.530 に答える
0

そんななら使えないと思います。IFステートメントは、制御フロー、現在はデータ選択を制御するためのものです。クエリをに変更します

IF exists (select * from test3)
BEGIN
--query all three
END
ELSE
BEGIN
--query just two
END
于 2010-04-05T18:24:38.787 に答える