best way to select data from one table or another

BuZz

I have two temporary tables, say #t1 and #t2 in Sql Server 2008. I need to create #t3 such as:

  • when #t1 has rows, indifferently of #t2's contents, #t3 = select * from #t1
  • when #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 ?

Gordon Linoff

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.

edited at
0

Comments

0 comments
Login to comment

Related

What is the best way in sql to select 3 users from data in another table?

What is the best way to pass data from one View to another in Xamarin?

what is the best way to select different data from same table in Laravel?

Select data from one table with the conditions from another

More Efficient Way to Copy Large Data from One Table to Another

Select data from one table based on selected rows in another

Best way to copy from one array to another

What is the best way to insert 6000000 records from one table to another table in ORACLE?

Data from one table to select data columns from another table, using r

How to select data from one table and select count to another table and combine it in MySQL?

Select from one table and count from another

How to select rows from one data.table to apply in another data.table?

SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table

Postgres: Best way to move data from public schema of one DB to new schema of another DB

Select data (join?) from only one table using an id from another table

Select data from one table that depends on values from another table in mysql

Select data from one table base on selection from another table in SQL

Append data from one table to another table

Copy Data from one table to another table

Efficient way to reference one table from another

How to select all data from one table and records from another table matching data in first selection. All in one query

What is best way to fetch data from two table with one to many relationship?

Select from one table if no records found in another

Select data from one table depending on the results of another select SQL Server

Select all data from one table that does not exists in another table given 3 key columns

select some data from one database table and insert into another table in the same database

Select data from one table & then rename the columns based on another table in SQL server

How to select data(with one decimal type) from a table and insert into another table (with different decimal type)

How to select data from one table and insert it into another table with a new column