sql - best way to select data from one table or another -


i have 2 temporary tables, #t1 , #t2 in sql server 2008. need create #t3 such as:

  • when #t1 has rows, indifferently of #t2's contents, #t3 = select * #t1
  • when #t1 has no rows, #t3 = select * #t2

we can assume #t1 , #t2 have same columns don't think rely on fact.

i thinking of draws logic out of 'if exists (select * ...)' statements, wouldn't there better sort of bool operators ?

the simplest way implement logic as:

if (exists (select * #t1)) begin     select *     #t3     #t1; end; else begin     select *     #t3     #t2; end; 

you can in 1 statement as:

select t.* #t3  ((select *         #t1        )        union        (select *         #t2         not exists(select * #t1)        )       ) t 

however, think explicit if clearer way express intent.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -