1

这是我的表定义:

tblTrans

PK - trans_id (int)
   - user_id_fk (int)
   - trans_created (datetime)
   - trans_amount (money)

我想汇总给定日期的所有销售数据,并将其返回为NumberOfTransactions,TotalDollars

我有以下 SQL 查询,我想将其移至以参数为参数的存储过程MM/DD/YYYY

查询有效,我只是不确定如何传入参数。

SELECT Cast(count(*) as varchar(8)) + ','
                         +  Cast(Sum(trans_amount) as varchar(8)) as SalesData 
FROM PD_Transaction 
WHERE pd_Trans_CreatedOn BETWEEN '20130122' 00:00:00.000' AND '20130122 23:59:59.997'

我想将其创建为存储过程,并仅传入日期。

谢谢!

4

3 回答 3

1

如果仅通过一个日期:

CREATE PROC P @Date DATE
AS BEGIN 
    SELECT Cast(count(*) as varchar(8)) + ',' +  Cast(Sum(trans_amount) as varchar(8)) as SalesData FROM PD_Transaction WHERE pd_Trans_CreatedOn
    BETWEEN CONVERT(datetime, CONVERT(VARCHAR(10), @Date, 121) + ' 00:00:00', 121) and CONVERT(datetime, CONVERT(VARCHAR(10), @Date, 121) + ' 23:59:59', 121)
END
于 2013-01-25T04:34:16.507 回答
0

我从您的查询中询问您使用 SQL Server 作为 RDBMS。这是存储的过程:

CREATE PROC P @startDate DATE, @endDate DATE
AS BEGIN 
    SELECT Cast(count(*) as varchar(8)) + ',' +  Cast(Sum(trans_amount) as varchar(8)) as SalesData FROM PD_Transaction WHERE pd_Trans_CreatedOn
    BETWEEN @startDate and @endDate
END
于 2013-01-25T04:31:25.083 回答
0

您可以在存储过程中声明两个参数,@StartDate 和 @EndDate 等。在您的代码中,使用

DateTime dtStart = new DateTime(2013,01,22);
DateTime dtEnd= new DateTime(2013,01,23);

然后 sql 脚本将类似于

*.....FROM PD_Transaction WHERE pd_Trans_CreatedOn >= @StartDate AND pd_Trans_CreatedOn < @EndDate*
于 2013-01-25T04:33:59.533 回答