存储过程根据从今天开始的 X 月数给出 "discount"

Stored procedure giving "discount" based on X number of months from todays date

所以我的问题是,如果产品 X 个月未售出,我必须制定一个程序,将我产品 table 的所有价格乘以 0.8。

目前我似乎无法比这更进一步:

GO
CREATE PROC newprice(@numberofmonth int)
AS
BEGIN
DECLARE @today datetime
SET @today = GetDate()
SELECT product.productid, product.name 
FROM orders JOIN orderitem on orderitem.orderid = orders.orderid
            JOIN product on product.productid = orderitem.productid
WHERE orders.orderdate > (SELECT DATEADD(month, -@numberofmonth, @today))

UPDATE product set price = price * 0.8 where  
END

希望一切都足够透明,无需任何进一步描述即可阅读和理解。我正在使用 SQL 服务器。

我不明白你的问题completly.i假设你需要看这个

    GO CREATE PROC newprice(@numberofmonth int) 
AS 
BEGIN 
DECLARE @today datetime 
SET @today = GetDate() 
UPDATE product set price = price * 0.8 FROM orders JOIN orderitem on orderitem.orderid = orders.orderid JOIN product on product.productid = orderitem.productid WHERE orders.orderdate >
(SELECT DATEADD(month, -@numberofmonth, @today))   
END
CREATE PROCEDURE newprice(@numberofmonth int)
AS
BEGIN

UPDATE product
SET price = price * 0.8
FROM  product 
JOIN orderitem on product.productid = orderitem.productid
JOIN orders on orderitem.orderid = orders.orderid
WHERE orders.orderdate > (DATEADD(month, -@numberofmonth, GETDATE()))

END

请检查是否满足您的需求:

DECLARE @today datetime
SET @today = GetDate()

Select * FROM product
--UPDATE product SET price = price * 0.8 
WHERE not exists (
    SELECT product.productid, product.name 
    FROM orders 
    INNER JOIN orderitem on orderitem.orderid = orders.orderid
    WHERE orders.orderdate > (SELECT DATEADD(month, -@numberofmonth, @today)))