小编use*_*277的帖子

联盟和如果存在 - 不合作 - 请帮助

如果没有从表返回的行,我需要获取虚拟值.If exists本身可以工作,但会给Union带来错误.有人可以指导我解决方案或解决方法吗?

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'
Run Code Online (Sandbox Code Playgroud)

sql-server union if-statement exists

2
推荐指数
1
解决办法
4941
查看次数

标签 统计

exists ×1

if-statement ×1

sql-server ×1

union ×1