I have two tables. Table A and Table B. Table A has some unique id's already existing. I want to insert a new custom row into Table A where those rows for my condition exist in Table B and not in Table A. How do I insert all rows from Table B that don't exist in Table A? I am looking for MSSQL solution.
I have tried the following two forms but neither seem to work:
Example A:
INSERT into TableA
(
Id,
Selection1,
Selection2,
Selection3
)
SELECT
(
Id, /* this field comes from TableB */
0,
0,
1
)
from TableB
where NOT EXISTS ( SELECT * FROM TableA
INNER JOIN TableB
ON TableA.Id = TableB.ID)
Example B
IF NOT EXISTS ( SELECT * FROM TableA
INNER JOIN TableB
ON TableA.Id = TableB.Id)
INSERT into TableA
(
Id,
Selection1,
Selection2,
Selection3
)
SELECT
(
Id, /* this field comes from TableB */
0,
0,
1
)
from TableB