2010-04-05 3 views
2

테이블에서 반환 된 행이없는 경우 더미 값을 가져와야합니다. If는 자체적으로 작동하지만 연합에 오류가 발생합니다. 누군가 나를 해결책이나 해결 방법으로 안내해 줄 수 있습니까?Union과 If Exists - 함께 작동하지 않음 - 도와주세요

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' 

답변

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) 
+0

으로 변경하십시오. 고맙습니다. – user267277

0

내가 그런 경우는 사용하지 수 있다고 생각합니다. IF 문은 제어 흐름을 제어하고 이제는 데이터를 선택하는 것입니다. 쿼리를

IF exists (select * from test3) 
BEGIN 
--query all three 
END 
ELSE 
BEGIN 
--query just two 
END 
관련 문제