I have two temporary tables, say #t1
and #t2
in Sql Server 2008. I need to create #t3
such as:
#t1
has rows, indifferently of #t2
's contents, #t3
= select * from #t1
#t1
has no rows, #t3
= select * from #t2
we can assume #t1
and #t2
have the same columns but I don't think I would like to rely on that fact.
I was thinking of something that draws some logic out of 'if exists (select * ...)
' statements, but wouldn't there be better like some sort of bool operators ?
The simplest way is to implement the logic as:
if (exists (select * from #t1))
begin
select *
into #t3
from #t1;
end;
else
begin
select *
into #t3
from #t2;
end;
You can do this in one statement as:
select t.*
into #t3
from ((select *
from #t1
)
union all
(select *
from #t2
where not exists(select * from #t1)
)
) t
However, I think the explicit if
is a clearer way to express your intent.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments