简体   繁体   中英

How to insert the result in the desired row? SQL Server

I need the result that I got to be inserted in the right row.

DECLARE @DiscPrice float;
SET @DiscPrice = (SELECT Prod.priceProd - Prod.priceProd / 100 * Prod.disc 
                  FROM Prod 
                  WHERE id_prod = 1);

UPDATE Prod 
SET priceDisc = @DiscPrice 
WHERE id_prod = 1;

SELECT * FROM Prod;

That is, instead of WHERE id_prod = 1 , there was something that inserted the desired result in all rows.

I'm not sure I made myself clear, but I hope that you will understand.

I think you want

UPDATE Prod 
SET priceDisc = ((priceProd - priceProd) / 100) * disc
WHERE id_prod = 1;

There is no need to use a variable or a query to assign the value to it.

You can directly update the query as below

UPDATE Prod SET priceDisc =((priceProd - priceProd) / 100 * disc)

This will update the data in all rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM