交叉表的实现

Posted on 2008-08-05 16:40  至今  阅读(174)  评论(0编辑  收藏  举报

declare @v_sql varchar(8000)
set @v_sql = ' '
select @v_sql = @v_sql + ',CASE WHEN object = ''' + object + ''' THEN fen else null end ' +'as '+object
  FROM cross_test
set @v_sql = 'select '+right(@v_sql,len(@v_sql)-2) + '  from cross_test'

print @v_sql
exec (@v_sql)

 

CREATE TABLE [dbo].[cross_test]
(
 [code] [nchar](10) COLLATE Chinese_PRC_BIN NOT NULL,
 [object] [varchar](50) COLLATE Chinese_PRC_BIN NOT NULL,
 [fen] [nchar](10) COLLATE Chinese_PRC_BIN NOT NULL
) ON [PRIMARY]

1          语文 20       
2          数学 20       
3          化学 30       
4          物理 40       
5          英语 10       
 

Copyright © 2024 至今
Powered by .NET 9.0 on Kubernetes