Inserting data from one table to another is usually as simple as:
SELECT * INTO A FROM B
But just out of curiosity,suppose I have two tables tbl_A
and tbl_B
. I have 100 records in tbl_B
and some 20 rows in tbl_A
(some of which might be common in both tables), I want to insert rows from tbl_B
into tbl_A
which are not already present in tbl_A'
Also, lets assume that both table have identity fields.
You can use NOT EXISTS
:
INSERT INTO tbl_A
SELECT IdCol, Col2, Col3
FROM dbo.tbl_B B
WHERE NOT EXISTS(SELECT 1 FROM tbl_A A2 WHERE A2.IdCol = B.IdCol)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments