这是我的表定义:
啧
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'
我想将其创建为存储过程,并仅传入日期。
谢谢!
如果只传递一个日期:
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
我从您的查询中使用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
您可以在 sproc 中声明两个参数,@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*