views:

82

answers:

4

Hi All, not sure why this is not working..

UPDATE 
    ust
SET  
    ust.isUnsubscribedFromSystemEmails = 1
FROM         
    UserSetting AS ust 
INNER JOIN
    [User] ON ust.userID = [User].userID 
AND 
    [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses)

In plain English, I am trying to set the isUnsubscribed field to unsubscribed where the userID in the UserSetting table equals the userID in the user table and where the emailAddress in the user table is not in a list of emails from another table. I can run a select on the isUnsubbed column using pretty much the same syntax and it works fine? thanks!

p.s. I've looked at other similar questions here and the syntax appears the same but obviously I'm missing something.

+2  A: 

Yep you've overlooked something.

The set statement cannot reference the alias on the left side of the set.

Try:

UPDATE  
    ust 
SET   
    isUnsubscribedFromSystemEmails = 1 
--select *
FROM          
    UserSetting AS ust  
INNER JOIN 
    [User] ON ust.userID = [User].userID  
WHERE [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses) 

I added the commented out select so you can check to see that you aregetting results set you wanted.

HLGEM
"Unable to parse query Text" Invalid object name 'ust'
toddm
select *FROM UserSetting AS ust INNER JOIN [User] ON ust.userID = [User].userID AND [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses) works just fine by the way! returns the expected results??
toddm
If the select worked fine, I'm at a loss. Why don;t you govie us some sample data from User settings, User and and bademailaddresses and we can test against the data you are expecting to have.
HLGEM
I tried what I gave you on my machine and it parses and runs. are you sure you used what I did exactly? (Ichanged the last and to a where but that is functionally the same thing and both versions work, I just prefer to use the join only for what is needed for the join). The key is changing the ust.isUnsubscribedFromSystemEmails = 1 to isUnsubscribedFromSystemEmails = 1
HLGEM
A: 

Try this :

UPDATE UserSetting ust SET usr.isUnsubscribedFromSystemEmails = 1
WHERE ust.emailAdress IN (select emailAddress from bademailAddresses);
ykatchou
i tried this.. for one the isUnsubbed column is in the ust table.. even with this change the query does not parse.
toddm
A: 

Try:

UPDATE  
    UserSetting
SET   
    isUnsubscribedFromSystemEmails = 1 
FROM          
    UserSetting 
INNER JOIN 
    [User] ON UserSetting.userID = [User].userID  
AND  
    [User].emailAddress IN (SELECT emailAddress FROM BadEmailAddresses)
Mark Bannister
this sets every single record in UserSetting.isUnsubscribedFromSystemEmails to true
toddm
So every single user email is bad?
HLGEM
+1  A: 

Although the UPDATE...FROM syntax is essential in some circumstances, I prefer to use subqueries whenever possible. Does this do what you need?

UPDATE UserSetting
SET isUnsubscribedFromSystemEmails = 1
WHERE userID in (SELECT userID from [User]
                WHERE emailAddress in (SELECT emailAddress FROM BadEmailAddresses))
Paul Spangle
this works. thanks! Subsequently updates the set twice though.. but at this point i could care less. thanks!
toddm