我有两个临时表,例如#t1
和#t2
在Sql Server 2008中。我需要创建#t3
例如:
#t1
有行,与#t2
的内容无关,#t3
=select * from #t1
#t1
没有行时,#t3
=select * from #t2
我们可以假定#t1
并#t2
具有相同的列,但我认为我不想依靠这一事实。
我当时在想一些东西可以从' if exists (select * ...)
'语句中提取出逻辑,但是会不会像布尔运算符那样更好呢?
最简单的方法是将逻辑实现为:
if (exists (select * from #t1))
begin
select *
into #t3
from #t1;
end;
else
begin
select *
into #t3
from #t2;
end;
您可以在一条语句中执行以下操作:
select t.*
into #t3
from ((select *
from #t1
)
union all
(select *
from #t2
where not exists(select * from #t1)
)
) t
但是,我认为明确表示if
是表达您意图的更清晰方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句