SQLServer:SQL实现动态交叉表
SET QUOTED _ IDENTIFIER ON
GO
SET ANSI _ NULLS ON
GO
ALTER procedure CrossTable
@ str tablename as varchar(50)= ' ',-查询表
@ strkolas varchar (50) = ' ',
@ strgroup as varchar (50) = ' ',-分组字段
@ strnumber as varchar (50) = ' ',-计数字段
。 @ strTempCol as varchar(100)
execute(' DECLARE corss _ CURSOR FOR SELECT DISTINCT '+@ str col+' from '+@ strTableName+' FOR read only ')-generate CURSOR
begin
set no counton
set @ strSql = ' SELECT '+@ str group+','+@ str computer+'('+@ strNumber+')as['+@ strNumber+']']'[/set +@ str compute+'(case '+@ str col+' when ' '+@ strTempCol+' ' ' then '+@ strNumber+' else 0 end)as['+@ strTempCol+']'
end
SET @ strSql = @ str SQL+' from '+@ strTableName+' group by '+@ strGroup
print @ strSql
execute(@ strSql)
0条评论