Insert Into SQL VBA

2019-03-06 21:35发布

问题:

I am trying to select records that are in the first table but not in the second table and insert them into the second table using a sql statement in VBA. I have started it below but I am not sure why it won't work. I am rather new to sql so any help would be greatly appreciated.

MySQL = "INSERT INTO Clients ()" & _
    "SELECT DISTINCT DD.[Client ID] " & _
    "FROM " & tableName & " as DD " & _
    "Where DD.[Client ID] NOT IN (SELECT DD.[Client ID] FROM " & tableName & " as DD)"

回答1:

First, you need supply field list to insert statement:

INSERT INTO Clients (ClientID)...

Second, your query doesn't insert any rows, because you check ClientID presence in same table. Did you mean someting like next:

"Where DD.[Client ID] NOT IN (SELECT DD2.[Client ID] FROM " & tableName2 & " as DD2)"