2

Here is the query that I have come up with but seems not working right.

UPDATE ItemDynamic
SET TaxID=8
FROM ItemDynamic
INNER JOIN Item
ON Item.ID = ItemDynamic.ItemID
WHERE Item.DateCreated>'2013-06-18' and StoreID=11 or StoreID=1 or StoreID=2 or StoreID=3

DateCreated column is not in the ItemDynamic, this column is in Item table. So I join Item table and ItemDynamic table. The problem is, when I tried to update more stores same time, it updates the whole item. But when I update only one store, it updates fine. Is there any reason why?

Thanks,

2
  • 3
    sounds like you just need brackets WHERE Item.DateCreated>'2013-06-18' and (StoreID=11 or StoreID=1 or StoreID=2 or StoreID=3) Commented Jun 19, 2013 at 21:14
  • Use StoreID IN (...) instead of a bunch of ORs Commented Jun 19, 2013 at 21:49

2 Answers 2

1

You have an error in your query, and your OR clause should be surrounded with round brackets

UPDATE ItemDynamic a 
INNER JOIN Item B
ON b.ID = a.ItemID 
SET a.TaxID=8 
WHERE b.DateCreated>'2013-06-18' 
AND (b.StoreID=11 OR b.StoreID=1 OR b.StoreID=2 OR b.StoreID=3)

I assumed StoreID and DateCreated are in table Item.

Sign up to request clarification or add additional context in comments.

1 Comment

INNER JOIN giving me an error. But I used a_horse_with_no_name gave me the correct answer! Thanks!
1

When you want to test a field against multiple values, I find IN to be clearer than a bunch of OR tests. And it avoids the AND/OR precedence problem that you ran into.

UPDATE ItemDynamic
INNER JOIN Item
SET TaxID=8
ON Item.ID = ItemDynamic.ItemID
WHERE Item.DateCreated>'2013-06-18'
AND StoreID IN (11, 1, 2, 3)

1 Comment

What was the error? And if you got an answer, you should post it for the benefit of others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.