在行中传输列

时间:2018-06-19 07:16:53

标签: sql sql-server

有一个用于创建表格的脚本。

create table #test_for_TRANSPOSE (id int, vol char(10), su int)

declare @num int 

set @num = 1

while @num <=50

begin

    insert into #test_for_TRANSPOSE (id, vol, su)

         values (@num, (select PATIENTS01.passport_data
           from  PATIENTS01                                                         
          where id_patients = @num),
                @num)                                                
        set @num = @num +1
   end

如何在行中传输列?

我试图这样做,但它不起作用:

create table #table_after_TRANSPOSE(new_vol varchar(15))

声明@num_transp int 声明@vol_transp char(15) 声明@count_rows int 声明@column_name char(15)

设置@num_transp = 1 设置@count_rows =(从#test_for_TRANSPOSE中选择count(id))

而@num_transp&lt; = @count_rows     开始         设置@column_name ='column'+ @num_transp         alter table #table_after_TRANSPOSE add @column_name char(10)         插入#table_after_TRANSPOSE(@column_name)              值((选择#test_for_TRANSPOSE.id                        来自#test_for_TRANSPOSE                       其中#test_for_TRANSPOSE.id = @num_transp),                       (选择#test_for_TRANSPOSE.vol                         来自#test_for_TRANSPOSE                       其中#test_for_TRANSPOSE.id = @num_transp
                      ))         设置@num_transp = @num_transp + 1       端

0 个答案:

没有答案