Reputation: 8135
My SQL query
UPDATE Task SET Done = 1 WHERE (ID = (SELECT ID FROM User WHERE UserName = @UserName)
I tried and got an error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
Is there any possible way to do that (for example a for loop)? Thanks
Upvotes: 2
Views: 970
Reputation: 771
UPDATE Task SET Done = 1 WHERE ID IN (SELECT ID FROM User WHERE UserName = @UserName)
Upvotes: 1
Reputation: 680
How about the following. You can use simple joins the same way you do in SELECT statements:
UPDATE Task t, User u
SET t.Done = 1
WHERE t.ID = u.ID
AND u.UserName = @UserName
Upvotes: 0
Reputation: 36999
You can use the IN
operator.
WHERE ID IN (SELECT ID FROM User WHERE UserName = @UserName)
Upvotes: 4
Reputation: 52372
How would a row's ID be equal to more than one value?
That doesn't make sense, which is why writing such a query isn't possible.
Checking if the ID is in a set would make sense, though...
...WHERE ID IN (SELECT ID...
Upvotes: 6