Append Table - add only new rows (no duplicates)

phinix

Registered User.
Local time
Today, 08:53
Joined
Jun 17, 2010
Messages
130
I must be tired or something, cause I cannot do something that is pretty simple:(

Two tables, table1 and table2.
Table2 has same rows as table1 + some new ones.
I want to append table1 with only new rows based on primary key field - ID.

So this query will add all rows from table2 to table1, right?

INSERT INTO table1
SELECT DISTINCT table2.*
FROM table2;

Now, how can I add all new rows that are not already in table1?:confused:

This apparently doesn't work:(

INSERT INTO table1
SELECT DISTINCT table2.*
FROM table2
WHERE table1.ID<>table2.ID;


Please help, cause I'm loosing reality here:eek:
 
Nothing easier.
Look at "DemoAddNewRowsA2002" (attachment, zip).
Look at tables.
Open form and try. Look at VBA.
 

Attachments

I think I just got it, left join and all ID that are not there:)

INSERT INTO table1
SELECT * FROM table2 LEFT JOIN table1 ON table2.ID = table1.ID
WHERE table1.ID Is Null;
 
Well, almost - I'm getting error - "Duplicate output destination ID" :(

Why is that? those rows, those ID fields actually don't exist in table1 so should be ok to add right?
 
Nothing easier.
Look at "DemoAddNewRowsA2002" (attachment, zip).
Look at tables.
Open form and try. Look at VBA.

That adds rows from one table to other deleted previous ones.
VBA just says copy object.

I would like to copy new rows from table 2 to table1 by adding only new ones to it.
 
Why do you have a DISTINCT clause in your first post?

Shouldn't it be something like this:
Code:
INSERT INTO table1 
SELECT DISTINCT table2.*
FROM table2
WHERE NOT Exists (SELECT * FROM tb1 FROM table1 AS tb1 WHERE tb1.ID = table2.ID);

OR:

1. Use the Unmatched query to find those records that don't exist in Table1
2. Using the query from step1 you INSERT into.
 
Why do you have a DISTINCT clause in your first post?

Shouldn't it be something like this:
Code:
INSERT INTO table1 
SELECT DISTINCT table2.*
FROM table2
WHERE NOT Exists (SELECT * FROM tb1 FROM table1 AS tb1 WHERE tb1.ID = table2.ID);
OR:

1. Use the Unmatched query to find those records that don't exist in Table1
2. Using the query from step1 you INSERT into.

I think I'm gonna have to start paying you:)
That worked fine, as ussusal! :)
I have NO IDEA why when I used "no exists" previously it didn't work.
As I said, I was loosing reality here, tried all I knew, including "no exists", nothing worked. I rewrote it and it worked, but only after you suggested it:D
Again, fantastic, thank you for help my friend.
 

Users who are viewing this thread

Back
Top Bottom