从存储过程中返回三列

时间:2013-07-17 09:47:05

标签: sql sql-server

我有一个存储过程,我想返回三列,它们只包含根据今天的日期,本周和当前月份的记录计数

SELECT count(*) as TotalCount 
FROM CalendarItem t 
LEFT JOIN company c ON t.companyID = c.companyID AND c.enabled = 1 
INNER JOIN CalendarItemType tt ON tt.CalendarItemtypeID = t.CalendarItemTypeID 
LEFT JOIN request r ON r.requestID=t.workrequestID 

where t.deleted = 0  + @where

当前我通过传递不同的arg

来执行这个Sp三个时间今天一周和一个月
@where =AND (t.startDate BETWEEN '2013-7-17' AND '2013-7-18')

和周

@where = AND (t.startDate BETWEEN '2013-7-14 00:00:00' AND '2013-7-20')

和月份一样

我希望我的SP根据今天的日期,星期和MOnth

返回三个计数

所以任何想法我应该怎么做

由于

2 个答案:

答案 0 :(得分:1)

将三个输出参数传递给存储过程,并将其值设置为sp,然后很容易将其取回

答案 1 :(得分:1)

请尝试:

SELECT 
    sum(case when t.startDate BETWEEN '2013-7-17' AND '2013-7-18' then 1 else 0 end) as DayCount,
    sum(case when t.startDate BETWEEN '2013-7-14' AND '2013-7-20' then 1 else 0 end) as WeekCount,
    //month condition here
FROM CalendarItem t 
LEFT JOIN company c ON t.companyID = c.companyID AND c.enabled = 1 
INNER JOIN CalendarItemType tt ON tt.CalendarItemtypeID = t.CalendarItemTypeID 
LEFT JOIN request r ON r.requestID=t.workrequestID 

where t.deleted = 0