SQLServer:SQL实现动态交叉表

SQLServer:SQL实现动态交叉表,第1张

SQLServer:SQL实现动态交叉表,第2张

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)

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » SQLServer:SQL实现动态交叉表

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情