August 18, 2010 at 2:17 pm
Newb here. I am trying to make a query with 50 or so "OR" conditions and know there must be a better way to accomplish my goal.
What it looks like now is:
UPDATE email_table
SET email_opt_out='Y'
WHERE email_addr='asdf@snarf.com' OR 'qwerty@reddit.com' (etc etc etc etc)
So I am looking for a way to either reference this list of email addresses in another form or to add them to the query without writing out a huge WHERE statement.
Any suggestions?
August 18, 2010 at 2:24 pm
ameuse 60211 (8/18/2010)
Newb here. I am trying to make a query with 50 or so "OR" conditions and know there must be a better way to accomplish my goal.What it looks like now is:
UPDATE email_table
SET email_opt_out='Y'
WHERE email_addr='asdf@snarf.com' OR 'qwerty@reddit.com' (etc etc etc etc)
So I am looking for a way to either reference this list of email addresses in another form or to add them to the query without writing out a huge WHERE statement.
Any suggestions?
Try:
WHERE email_addr IN ('asdf@snarf.com', 'qwerty@reddit.com', . . . ) (etc.)
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
August 18, 2010 at 2:33 pm
I would suggest adding the email addresses to a temp table, then do the update by joining on that.
i.e.
CREATE TABLE #tempEmail (EmailAddress varchar(100))
INSERT INTO #tempEmail
SELECT 'asdf@snarf.com' UNION ALL
SELECT 'qwerty@reddit.com'
UPDATE et
SET email_opt_out='Y'
FROM email_table et
JOIN #tempEmail te
ON te.email_addr = et.EmailAddress
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 19, 2010 at 6:26 am
Thanks for the tips, I haven't ever used IN.
You wouldnt happen to know of a way I could do it similar to this with all the emails in a txt file...
for N in email_addr.txt do
UPDATE email.table
SET email_optout='Y'
WHERE email_addr=$N
August 19, 2010 at 6:39 am
Create a temp table and do a bulk insert from the txt file. Then do as the above suggested, joining the tables together.
Create table #emailaddys (address varchar(100))
bulk insert #emailaddys from '<path to file>'
<update statement from above here>
August 20, 2010 at 6:35 am
Unless the email addresses were constantly changing, I would store them in a permanent table, then do the join as suggested. This would allow you to change the table as needed, and not have to bulk insert each time. If the email addresses are constantly changing (ftp once a week, etc.), then bulk insert to temp, and then a join is the way to go.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply