AOB
Registered User.
- Local time
- Today, 20:15
- Joined
- Sep 26, 2012
- Messages
- 637
Hi guys,
Probably a dumb question but can't figure it out
I have a simple INSERT INTO statement, to add records to my main table from a series of linked tables (text files). I need to run the same INSERT INTO multiple times so I can eventually drop the linked text and just work off my normalised tables.
However - I only want to add new records. So if a particular combination already exists, I want the SQL to exclude those records from the INSERT INTO.
Normally I would just use something like :
But in this particular situation, the INSERT INTO is a combination of fields from different tables with various joins so it becomes more cumbersome.
Here is the basic SQL I have already :
Now this will work, because I also have composite primary keys across the three fields in the main (tblAccounts) table. So duplicates will be prevented that way. But my autonumbers go right out of whack and I'd prefer to have them properly incremented for each genuinely new account combination.
So can anybody tell me how I can adapt the above query to only attempt to insert new records, and exclude the duplicates 'up-front'? Pretty sure it involves some kind of WHERE EXISTS but haven't the foggiest how to syntax it?...
Thanks!
Al
Probably a dumb question but can't figure it out
I have a simple INSERT INTO statement, to add records to my main table from a series of linked tables (text files). I need to run the same INSERT INTO multiple times so I can eventually drop the linked text and just work off my normalised tables.
However - I only want to add new records. So if a particular combination already exists, I want the SQL to exclude those records from the INSERT INTO.
Normally I would just use something like :
Code:
INSERT INTO tblMain
SELECT DISTINCT ltbLink.fldField AS fldField
FROM ltbLink
WHERE ltbLink.fldField Not In (SELECT tblMain.fldField FROM tblMain)
But in this particular situation, the INSERT INTO is a combination of fields from different tables with various joins so it becomes more cumbersome.
Here is the basic SQL I have already :
Code:
INSERT INTO tblAccounts
SELECT DISTINCT TRIM(ltb1.[BANK ACCOUNT]) AS AccountNumber, tblCurrencies.CurrencyID AS CurrencyID, tblBanks.BankID AS BankID
FROM ((ltb1
INNER JOIN tblCurrencies
ON TRIM(ltb1.[CURR CODE])=tblCurrencies.CurrencyCode)
INNER JOIN ltbDepBankLookup
ON TRIM(ltb1.[BANK ACCOUNT])=ltbBankLookup.[BANK ACCOUNT])
INNER JOIN tblBanks
ON TRIM(ltbBankLookup.[AGENT BANK])=tblBanks.BankAcronym
Now this will work, because I also have composite primary keys across the three fields in the main (tblAccounts) table. So duplicates will be prevented that way. But my autonumbers go right out of whack and I'd prefer to have them properly incremented for each genuinely new account combination.
So can anybody tell me how I can adapt the above query to only attempt to insert new records, and exclude the duplicates 'up-front'? Pretty sure it involves some kind of WHERE EXISTS but haven't the foggiest how to syntax it?...
Thanks!
Al