执行存储过程时使用WHERE子句

时间:2017-01-21 01:58:38

标签: sql sql-server stored-procedures

我通过加入其他表格来查看数据交易的存储过程

create procedure prRecapitulation
as
begin
    SELECT      a.TransactionID,
                a.Buyer_Code,
                a.Date_Transaction,
                b.NuggetID,
                SUM(b.Qty) as Qty,
                SUM(b.Sub_Total) as Sub_Total,
                SUM(a.Total_Price) as Total_Price
    FROM        Transactions a JOIN DetailTransaction b On a.TransactionID = b.TransactionID
    WHERE       DAY(a.Date_Transaction) = 21 AND MONTH(a.Date_Transaction) = 01 AND YEAR(a.Date_Transaction) = 2017
    GROUP BY    a.TransactionID, a.Buyer_Code, a.Date_Transaction, b.NuggetID
    ORDER BY    a.Date_Transaction DESC
end

但是我想从存储过程中删除WHERE子句,因为某些时候(可能)条件会根据数字21,01和2017而改变。

这是我的期望查询

exec prRecapitulation
where DAY(a.Date_Transaction) = 21

因此,该程序可以根据日期21显示交易,也可以使用MONTH()YEAR()功能。

4 个答案:

答案 0 :(得分:3)

将值作为参数传递:

create procedure prRecapitulation (
    @year int,
    @month int,
    @day int
) as
begin
    SELECT a.TransactionID, a.Buyer_Code, a.Date_Transaction, b.NuggetID,
           SUM(b.Qty) as Qty, SUM(b.Sub_Total) as Sub_Total,
           SUM(a.Total_Price) as Total_Price
    FROM Transactions a JOIN
         DetailTransaction b
         On a.TransactionID = b.TransactionID
    WHERE DAY(a.Date_Transaction) = @day AND
          MONTH(a.Date_Transaction) = @month AND
          YEAR(a.Date_Transaction) = @year
    GROUP BY a.TransactionID, a.Buyer_Code, a.Date_Transaction, b.NuggetID
    ORDER BY a.Date_Transaction DESC
end;

然后使用显式值调用:

exec prRecapitulation(2017, 1, 21);

如果需要,您还可以提供参数默认值。

编辑:

对于此类查询,我实际上建议您使用date数据类型提供日期:

create procedure prRecapitulation (
    @date date
) as
begin
    SELECT a.TransactionID, a.Buyer_Code, a.Date_Transaction, b.NuggetID,
           SUM(b.Qty) as Qty, SUM(b.Sub_Total) as Sub_Total,
           SUM(a.Total_Price) as Total_Price
    FROM Transactions a JOIN
         DetailTransaction b
         On a.TransactionID = b.TransactionID
    WHERE a.Date_Transaction = @date
    GROUP BY a.TransactionID, a.Buyer_Code, a.Date_Transaction, b.NuggetID
    ORDER BY a.Date_Transaction DESC
end;

您可以使用以下方式调用它:

exec prRecapitulation('2017-01-21');

您还可以修改存储过程以提供两个日期,这些日期可用作范围。

答案 1 :(得分:3)

我会使用表值函数而不是带有三个输入参数的过程。通过表格调用和加入将很容易。

CREATE FUNCTION Udf_prrecapitulation(@day   tinyint,
                                     @month tinyint,
                                     @year  smallint)
RETURNS TABLE
AS
    RETURN
      (SELECT a.TransactionID,
              a.Buyer_Code,
              a.Date_Transaction,
              b.NuggetID,
              Sum(b.Qty)         AS Qty,
              Sum(b.Sub_Total)   AS Sub_Total,
              Sum(a.Total_Price) AS Total_Price
       FROM   Transactions a
              JOIN DetailTransaction b
                ON a.TransactionID = b.TransactionID
       WHERE  ( Day(a.Date_Transaction) = @day
                 OR @day IS NULL )
              AND ( Month(a.Date_Transaction) = @month
                     OR @month IS NULL )
              AND ( Year(a.Date_Transaction) = @year
                     OR @year IS NULL )
       GROUP  BY a.TransactionID,
                 a.Buyer_Code,
                 a.Date_Transaction,
                 b.NuggetID) 

致电

SELECT *
FROM   dbo.Udf_prrecapitulation(21, 1, 2017) 

答案 2 :(得分:1)

使用适当索引(并且效率更高)的更好方法:

create procedure prRecapitulation 
(
    @year int,
    @month int,
    @day int
) 
as
begin
    declare @date as date
    set @date = cast(cast(@year as char(4)) + '-' + cast(@month as char(2)) + '-' + cast(@day as char(2)) as date)
    SELECT     
        a.TransactionID,
        a.Buyer_Code,
        a.Date_Transaction,
        b.NuggetID,
        Qty = SUM(b.Qty),
        Sub_Total = SUM(b.Sub_Total),
        Total_Price = SUM(a.Total_Price)
    FROM 
        Transactions a 
        JOIN DetailTransaction b ON a.TransactionID = b.TransactionID
    WHERE
        a.Date_Transaction between @date and Dateadd(ms, -3.33, Dateadd(day, 1, @date))
    GROUP BY 
        a.TransactionID, a.Buyer_Code, a.Date_Transaction, b.NuggetID
    ORDER BY 
        a.Date_Transaction DESC
end;

或者更好的是,只需将日期作为参数传递。

答案 3 :(得分:0)

 SELECT * 
 FROM OPENROWSET('SQLNCLI', 
   'server=(local);trusted_connection=yes', 'EXEC prRecapitulation') 
 where DAY(a.Date_Transaction) = 21