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