存储过程给予"折扣"基于从今天起的X个月数

时间:2016-11-30 16:14:09

标签: sql sql-server tsql stored-procedures dateadd

所以我的问题是,我必须制作一个程序,如果产品没有卖出X个月,我的产品表中的所有价格都会增加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 Server。

3 个答案:

答案 0 :(得分:0)

我完全不理解你的问题。我只是假设你需要看这个

    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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

请检查它是否符合您的要求:

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)))