create procedure dbo.statGradeAttendance
(
@beginTime datetime,
        @endTime   dateTime,
        @classID   varchar,
        @type      varchar)
as
set nocount onbegindeclare @sql varchar(8000)set @sql='select theTime'
select @sql=@sql+',sum(case category when '''+category +''' then 1 else 0 end) ['+category+']'+'from (select distinct category from tbAttendance) as a ' 
select @sql=@sql+' from tbAttendance
where '+@classID+'
 and theTime between '''+convert(varchar,@beginTime,120)+''' and '''+convert(varchar,@endTime,120)+'''
 and ifEnd='''+@type+''' group by theTime'
exec(@sql)
endGOexec statGradeAttendance '2004-1-30','2004-2-1', ' (classID=47 or classID=48) ','T'