交叉表的实现

Posted on 2008-08-05 16:40  至今  阅读(122)  评论(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       
 

posts - 34, comments - 79, trackbacks - 0, articles - 5

Copyright © 2020 至今
Powered by .NET Core on Kubernetes