0
IF EXISTS ( SELECT *
    FROM INFORMATION_SCHEMA.ROUTINES WHERE SPECIFIC_SCHEMA = N'dbo'
    AND SPECIFIC_NAME = N'Reports'
    AND ROUTINE_TYPE = N'PROCEDURE')
    DROP PROCEDURE dbo.Reports        
GO

CREATE PROCEDURE dbo.Reports (@Date_of_Purchase DATE)
AS
BEGIN
    SELECT o.O_Id, o.Sale_Price, o.Private_Band, c.Type, c.Points_Issued, o.Date_of_Purchase
    FROM Order_Details o
    LEFT JOIN Transaction_Historys c ON o.Date_of_Purchase = c.Date_of_Purchase
    WHERE o.Date_of_Purchase > @Date_of_purchase
END
GO

EXECUTE dbo.Reports
GO

我想做o.Date_of_Purchase > DATEADD(d,@date,getdate())@date应该是基于用户输入的动态值,如何@date动态声明 days()。

执行存储过程时出现错误:

必须声明标量变量

4

1 回答 1

0

以下是如何创建和运行一个过程,该过程采用天数的参数,将这些天数添加到今天的日期,然后针对该日期执行查询过滤。

重点是您需要创建、初始化和传入参数。

CREATE PROCEDURE dbo.Reports
(
    @Days INT
)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT o.O_Id, o.Sale_Price, o.Private_Band, c.Type, c.Points_Issued, o.Date_of_Purchase
    FROM Order_Details o
    LEFT JOIN Transaction_Historys c ON o.Date_of_Purchase = c.Date_of_Purchase
    WHERE o.Date_of_Purchase > DATEADD(DAY, @Days, CURRENT_TIMESTAMP);

    RETURN 0;
END
GO

-- Create and initialise parameter.
DECLARE @Days INT = 90;

-- Execute SP, passing in our parameter
EXECUTE dbo.Reports @Days;
于 2020-05-25T11:06:53.033 回答