2

I have a Itemmaster table and want to update the price from the itemmaster table into DailySales table.

ItemMaster (ItemName, Price)

DailySales (Date, ItemName, Price)

My query is:

update DailySales a 
set Price = (select b.price 
             from DailySales a, Itemmaster b where a.itemname = b.itemname)

The above query fails. Thanks in advance

2
  • @Qinnovator: please update your original question with the error message by editing it - don't post longer text here in comments! Really hard to read.... Commented Nov 26, 2011 at 14:06
  • this link is useful stackoverflow.com/questions/224732/… Commented Nov 26, 2011 at 14:08

3 Answers 3

3

You didn't tell us much about how it fails (error message or anything) - but I believe this statement ought to work:

UPDATE ds
SET ds.Price = im.Price
FROM dbo.DailySales ds
INNER JOIN dbo.Itemmaster im ON im.itemname = ds.itemname

Points to remember:

  • put your table name you want to update into the FROM clause and give it a meaningful table alias (not just a or b) - use that table alias in the UPDATE statement
  • use INNER JOIN (instead of "embedded" JOIN by just having table after table, comma-separated) to spell out your JOIN intention and your JOIN conditions
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for informing me about the alias name to be used
1

I assume your query fail because you are trying to assign result set (returned by SELECT) to a single column value. You need to use INNER JOIN to specify how each single row from ItemMaster correspond to a row from DailySales table, then you would be able to assign a Price value for the appropriate entries:

UPDATE DailySales
  SET ds.Price = im.Price
FROM DailySales ds
INNER JOIN ItemMaster im ON ds.ItemName = im.ItemName

Comments

0
update  a
set     Price = b.Price
from    DailySales a
        join ItemMaster b on a.ItemName = b.ItemName

1 Comment

Agree with marc_s that using a and b isn't particularly useful, but I thought it might help the OP understand it better.

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.