日期:2014-05-17 浏览次数:20698 次
Create table Table_3 (id int, date1 date, score int)
insert into table_3 select 1, '2013-09-01',1 union all select 2,'2013-09-02',2
union all select 3 ,'2013-09-02', 2 union all select 4,'2013-09-02', 2 union all select 5,'2013-09-03', 3 union all select 6,'2013-09-03', 4
go
create proc usp_test @TableName varchar(100), @field varchar(50), @filter varchar(300), @func varchar(10)
as
begin
Declare @sql nvarchar(max)
declare @filterSql varchar(200)
if(@filter !='')
begin
set @filterSql = ' and '+@filter
end
set @sql = 'select '+ @field+','+@func + '(score) from '+ @TableName + ' where 1=1 ' + @filterSql + ' group by '+@field
exec sp_executesql @sql
End
go
usp_test 'Table_3','date1', 'Convert(date, Date1) =''2013-09-02''','sum'
use test
Create table Table_3 (id int, date1 datetime, score int)
insert into table_3 select 1, '2013-09-01',1