Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query for information *not* in database

Tags:

sql

sql-server

I have a large list of email addresses, and I need to determine which of them are not already in my database. Can I construct a query that will tell me this information?

I am using SQL Server 2000, if it requires non-standard extensions

like image 370
Gabe Moothart Avatar asked Apr 08 '26 09:04

Gabe Moothart


2 Answers

make a temporary table, load the e-mailaddresses into the temporary table and then do a NOT IN query such as

SELECT emailaddress FROM temp_table WHERE emailaddress NOT IN (SELECT emailaddress FROM table)

you could extend that with an INSERT INTO

like image 123
jao Avatar answered Apr 10 '26 21:04

jao


For a huge list, I would recommend loading that list into a second table (e.g., TEMP_EMAIL_ADDRESS), then use:

SELECT
  EMAIL
FROM
  TEMP_EMAIL_ADDRESS
WHERE
  EMAIL NOT IN (SELECT EMAIL FROM EMAIL_ADDRESS)

Data Transformation

If your data is in a text file named emails.txt (one row per line), you can create the insert statements using the following DOS command:

FOR /F %i IN (emails.txt) DO echo INSERT INTO TEMP_EMAIL_ADDRESS (EMAIL) VALUES ('%i') >> insert-email.sql

That command will create a new file called insert-email.sql in the current directory, containing all the inserts you need to inject the existing list of e-mail addresses into the database.

like image 23
Dave Jarvis Avatar answered Apr 10 '26 21:04

Dave Jarvis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!