Copying query results to an existing table

hello there,

below is two tables i have:
table b20

| num | name |
| 1 | ANTONIUS |
| 2 | JAMES |
| 3 | LORENZO |

table b21

| num | name |
| 4 | WADE |
| 5 | JOHN |
| 6 | JAKE |

The goal is to unify the tables

Aftert the query, this is how Table 21 will look

| num | name |
| 1 | ANTONIUS |
| 2 | JAMES |
| 3 | LORENZO |
| 4 | WADE |
| 5 | JOHN |
| 6 | JAKE |

below is tne tables script

create table b20
(num float,name nvarchar(50))

insert into b20 SELECT 1,'ANTONIIS'
insert into b20 SELECT 2,'JAMES'
insert into b20 SELECT 3,'LORENZO'

create table b21
(num float,name nvarchar(50))

insert into b21 SELECT 4,'WADE'
insert into b21 SELECT 5,'JOHN'
insert into b21 SELECT 6,'JAKE'

If you just want to copy b20 into b21 you can use a simple INSERT statement

INSERT INTO b21 ([num], [name])
SELECT [num], [name]
FROM b20;

But you can use UNION to combine to tables:

SELECT num, name FROM b20
UNION
SELECT num, name FROM b21

There is a diffrence between UNION and UNION ALL. With UNION ALL you can have duplicates.

UNION (Transact-SQL) - SQL Server | Microsoft Learn

3 Likes

The second alternative does not update the relevant table.

The first alternative answers exactly the needs.

Thanks for your response

You could also use EXCEPT to insert into B21 all records from B20 that are not already in B21.

INSERT INTO B21
(
	NUM
	, NAME
)
SELECT NUM, NAME FROM B20
EXCEPT
SELECT NUM, NAME FROM B21
1 Like