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

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

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.

Thanks!

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,536 questions
{count} votes

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

    Hi,@newbie
    212767-image.png
    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