代码
Alter PROCEDURE groupby_table_splitFieldname
@groupby varchar(50),@tablename varchar(50),@fieldname varchar(50),@filter varchar(1000) = ''
AS
BEGIN
SET NOCOUNT ON;
declare @sql nvarchar(2000)
if @filter<>''
set @filter='and ('+@filter+')'
set @sql='declare @quote varchar(10)
select @quote='',''
select '+@groupby+' as caption,count('+@groupby+') as total,stuff((select @quote+['+@fieldname+'] from '+@tablename+' where '+@groupby+'=x.'+@groupby+' order by '+@fieldname+' for xml path('''')),1,len(@quote),'''') as '+@fieldname+' from '+@tablename+' x where 1=1 '+@filter+' group by '+@groupby
print @sql
exec sp_executesql @sql
END
GO
@groupby varchar(50),@tablename varchar(50),@fieldname varchar(50),@filter varchar(1000) = ''
AS
BEGIN
SET NOCOUNT ON;
declare @sql nvarchar(2000)
if @filter<>''
set @filter='and ('+@filter+')'
set @sql='declare @quote varchar(10)
select @quote='',''
select '+@groupby+' as caption,count('+@groupby+') as total,stuff((select @quote+['+@fieldname+'] from '+@tablename+' where '+@groupby+'=x.'+@groupby+' order by '+@fieldname+' for xml path('''')),1,len(@quote),'''') as '+@fieldname+' from '+@tablename+' x where 1=1 '+@filter+' group by '+@groupby
print @sql
exec sp_executesql @sql
END
GO