whether it is possible to overwrite the result of the UNION operation to the name of the first table

50 Views Asked by At

I have the following question. Can the result of a UNION operation on two tables overwrite the name of 1 table.

So I have

SELECT * FROM table1

UNION

SELECT * FROM table 2

I want this result of the UNION operation to overwrite table1. Is it possible ?

1

There are 1 best solutions below

1
Juan Pablo On

Assuming that table1 and table2 have the same columns:

declare @tbl table (DEFINE_SAME_COLUMNS_AS_table1);

insert into @tbl
SELECT * FROM table1
UNION 
SELECT * FROM table2

delete table1

insert into table1
select * from @tbl