What is the most efficient way to write an SQL statement?

asked 2022-06-19T13:58:42.917+00:00
newbie 81 Reputation points

HI, teams,

In SQL in Excel or SQL in Access,update statements are used to update multiple data sets in batches.
If table A and table B have the same structure, as follows:ID field, data field 1, data field 2, data field N

Where, the ID field primary key (set index in access). The data field has N columns,N is greater than 2 and less than 20
Suppose that table A now has 100,000 entries;There are 50,000 pieces of data in table B, 10,000 of which are new ID fields that table A does not have, 20,000 pieces of which are existing in table A but at least one corresponding data field has changed, and 20,000 pieces of which are existing in table A but all the data fields have not changed at all.


{count} votes

Accepted answer
  1. answered 2022-06-20T06:16:02.657+00:00
    Bert Zhou-msft 3,381 Reputation points

    From your description alone, it seems that the demand is not clear,If you are using SQL server, have you tried merge into, try to design code like:

     INSERT tbl_A (col, col2)  
    SELECT col, col2  
    FROM tbl_B  
    WHERE NOT EXISTS (SELECT col FROM tbl_A A2 WHERE A2.col = tbl_B.col);  

    If it doesn't work, tag access for more precise help.

    Bert Zhou

0 additional answers

Sort by: Most helpful