INSERT only the line not found

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

INSERT only the line not found

Post by sal21 »

Have a 2 table with 23 fileds. The field1 of table1 and table2 have a unique id.
I want to insert from table1 to table2 only the records not found base the filed1.
I effect i wanto match all unique id of table1 with unique id from the table2.
How to via vbcode and adojet????

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: INSERT only the line not found

Post by HansV »

Execute the following SQL string:

strSQL = "INSERT INTO Table2 SELECT * FROM Table1 WHERE Table1.ID Not In (SELECT Table2.ID FROM Table2)"

Substitute the correct names of Table1, Table2 and the ID field.
Last edited by HansV on 04 May 2010, 13:52, edited 1 time in total.
Reason: to correct omission (Table.ID > Table2.ID)
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: INSERT only the line not found

Post by sal21 »

HansV wrote:Execute the following SQL string:

strSQL = "INSERT INTO Table2 SELECT * FROM Table1 WHERE Table1.ID Not In (SELECT Table.ID FROM Table2)"

Substitute the correct names of Table1, Table2 and the ID field.

(SELECT Table.ID FROM Table2)"
Table is table2?

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: INSERT only the line not found

Post by HansV »

Yes, sorry.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: INSERT only the line not found

Post by sal21 »

HansV wrote:Yes, sorry.
PROB....
In in the table tath recive the new records in ID have a value complete but in table for mathching i need to join 2 filed to make the unique ID :groan: :sad: :scratch:

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: INSERT only the line not found

Post by HansV »

Please provide enough details for us to understand the problem. Thanks in advance.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: INSERT only the line not found

Post by sal21 »

HansV wrote:Please provide enough details for us to understand the problem. Thanks in advance.
sorry for my bad enghlish!

In effect in table1 have ID="1234567890" instead in table2 have the matching is a from joint from filed1&filed2 similar filed1=12345 filed2=67890....

I hope understand now, sorry.

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: INSERT only the line not found

Post by HansV »

Try

strSQL = "INSERT INTO Table2 SELECT * FROM Table1 WHERE Table1.ID Not In (SELECT Table2.Field1 & Table2.Field2 FROM Table2)"

If the fields of Table1 and Table2 are not identical, you may have to specify them explicitly:

strSQL = "INSERT INTO Table2 (Field3, Field4, Field5, Field6) SELECT FieldA, FieldB, FieldC, FieldD FROM Table1 WHERE Table1.ID Not In (SELECT Table2.Field1 & Table2.Field2 FROM Table2)"

substituting the appropriate names.
Best wishes,
Hans