Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
update Products_TBL set Product_Price=(
SELECT sum(rn.Amount) / sum(rn.Quantity) AS Result
FROM  Receipt_NoteDetalisTBL as rn
GROUP BY Product_Id
having rn.Product_Id between 1 and 1500

)
where Product_Id between 1 and 1500


this  sql erorr ;
Msg 512, Level 16, State 1, Line 1
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.
Posted
Updated 12-Aug-15 22:45pm
v2

Hi please use this query which give you your required output ie, this query will update rows from productid 1 to 1500 in a loop.


Thanks in advance.


DECLARE @a INT 

SET @a = 1

WHILE @a < 1501
BEGIN

exec sp_executesql N'update Products_TBL set Product_Price=(
SELECT sum(rn.Amount) / sum(rn.Quantity) AS Result
FROM Receipt_NoteDetalisTBL as rn
GROUP BY Product_Id
having rn.Product_Id=@a) where Product_Id=@a'   

SET @a = @a + 1
END
 
Share this answer
 
v2
The error message is a big clue here: the query returns more than one value - which you'd expect, given the GROUP clause - and you are trying to insert several different values into a single row. That won't work.

I'm not sure exactly what you are trying to do, but one way round might be to return just the TOP 1 result from your inner select.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900