日期:2014-05-19  浏览次数:20615 次

请教大虾们一个问题,谢谢
我现在有一个二维表(oldscore)如下:


xh                             高等数学       大学语文     经济学基础
051000333                 55                       67                   88
021000224                 64                       32                   75
041000851                 69                       75                   65

我现在要转换成一维表(newscore)如下
xh                         kc           cj
051000333   高等数学       55
051000333   大学语文       67
051000333   经济学基础   88
021000224   高等数学       64
021000224   大学语文       32
021000224   经济学基础   75
041000851   高等数学       69
041000851   大学语文       75
041000851   经济学基础   65


请问我要如何写,谢谢



------解决方案--------------------
try:

create table t_course
(
xh varchar(9) ,
gdsx int,
dxyw int,
jjxjs int
)

--
insert into t_course
select '051000333 ', 55 , 67 , 88 union
select '021000224 ', 64 , 32 , 75 union
select '041000851 ', 69 , 75 , 65

select * from t_course
--
declare @s varchar(8000)
declare @s_cur varchar(8000)
set @s = ' '


declare cur1 cursor for select name
from syscolumns where id = object_id( 't_course ') and name <> 'xh '

open cur1

fetch next from cur1 into @s_cur

while @@fetch_status = 0
begin
select @s =@s + 'select xh , ' ' ' + @s_cur + ' ' ' as kc , '+ @s_cur + ' as cj from t_course union '
fetch next from cur1 into @s_cur
end


close cur1
deallocate cur1


select @s = substring(@s , 1 ,len(@s) - 5 )

print @s

--sp_executesql N '@s '

exec ( @s)