SQL 程序按给定百分比计算列

SQL Procedure calculate a column by given percentage

我有一个 table 列:ProductCode、productLine、Saleprice。我需要编写程序根据特定产品线的给定数字计算新的销售价格,然后也更新此 table。

@newprice = (SELECT Saleprice FROM temp_table WHERE productLine = @line)* @percentage;

update temp_table
Saleprice = @newprice WHERE productLine = @line

我该怎么做?

您可以使用UPDATE直接查询和更新Saleprice

UPDATE t
SET    Saleprice = t.Saleprice  * @percentage
FROM   temp_table t
WHERE  t.productLine = @line

如果需要创建存储过程

CREATE PROCEDURE markup_salesprice
    @line int,
    @percentage decimal(10,2)
AS
BEGIN

  UPDATE t
  SET    Saleprice = t.Saleprice  * @percentage
  FROM   temp_table t
  WHERE  t.productLine = @line

END