如何在SQL中将垂直数据制作成水平数据(动态列)

时间:2014-09-19 04:11:13

标签: sql sql-server

查询:

SELECT Parameter_name,Data_Type, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.PARAMETERS
WHERE SPECIFIC_NAME = 'SP_Name'

结果:

Parameter_name            Data_Type              CHARACTER_MAXIMUM_LENGTH
--------------------------------------------------------------------------
@SubPortfolioCd           varchar                         3
@AsofDate                 varchar                         10
@CompareWith              varchar                         1
@CurrencyCd               varchar                         3

我想要结果为:

Column1            Column2      Column3         Column4           
---------------------------------------------------------------
@SubPortfolioCd    @AsofDate    @CompareWith    @CurrencyCd      
varchar            varchar      varchar         varchar
3                  10           1               3

请帮我在SQL Server 2008中实现上述结果... (动态列,如果它有很多参数) 感谢

2 个答案:

答案 0 :(得分:1)

这是一个经典问题,我们可以交叉应用和取消数据,然后使用PIVOT将其转换为水平数据

SELECT [Col1],[Col2],[Col3]  FROM
(SELECT Parameter_name,Data_Type, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.PARAMETERS
WHERE SPECIFIC_NAME = 'SP_NAME'
)T
cross apply 
( select 'Col1', T.Parameter_name  union
  select 'Col2', T.Data_Type union
  select 'Col3', cast(T.CHARACTER_MAXIMUM_LENGTH as nvarchar(256))
) c(col,val)
pivot
( max(val) for col in ( [Col1],[Col2],[Col3] ) ) pvt

答案 1 :(得分:0)

试试这个

declare @TotalRows int

DECLARE @Rows Table
(
 RowIndex Int PRIMARY KEY IDENTITY(1,1),
 Parameter_name sysname,
 Data_Type nvarchar(128), 
 CHARACTER_MAXIMUM_LENGTH int 
)


insert into @Rows(Parameter_name,Data_Type, CHARACTER_MAXIMUM_LENGTH)
 SELECT   Parameter_name,Data_Type, CHARACTER_MAXIMUM_LENGTH
 FROM INFORMATION_SCHEMA.PARAMETERS
 WHERE SPECIFIC_NAME = 'SP_Name'
Select @TotalRows=@@ROWCOUNT

  Declare @MaxLen1 int,@MaxLen2 int
  select  @MaxLen1=Max(Len(Parameter_name)) , @MaxLen2=Max(Len(Data_Type))  from @Rows
  Declare @MaxColSize int
  Select @MaxColSize= Case When @MaxLen1>@MaxLen2 Then @MaxLen1 Else @MaxLen2 End


declare @SQL nvarchar(MAX)
set @SQL='Declare @Table Table(
RowIndex int'

declare @I int
set @I=0
While @I < @TotalRows
Begin   
  set @SQL=@SQL+', 
Column'+Cast(@I+1 as nvarchar(5)) + ' nvarchar('+Cast(@MaxColSize as nvarchar(3))+')'
  Set @I=@I+1
End
 set @SQL=@SQL+'
 )
 insert into @Table (RowIndex) values(1),(2),(3) '

Declare 
 @Parameter_name sysname,
 @Data_Type nvarchar(50), 
 @CHARACTER_MAXIMUM_LENGTH int
Declare @RowIndex int
set @RowIndex=1
While @RowIndex < @TotalRows+1
Begin   

   select @Parameter_name =Parameter_name ,@Data_Type =Data_Type ,@CHARACTER_MAXIMUM_LENGTH =CHARACTER_MAXIMUM_LENGTH from @Rows Where RowIndex=@RowIndex
   set @SQL=@SQL+'
Update @Table Set Column'+Cast(@RowIndex as nvarchar(5))+'='''+Replace(@Parameter_name,'''','''''')+''' Where RowIndex=1
Update @Table Set Column'+Cast(@RowIndex as nvarchar(5))+'='''+Replace(ISNULL(@Data_Type,''),'''','''''')+''' Where RowIndex=2
Update @Table Set Column'+Cast(@RowIndex as nvarchar(5))+'='+isNull(Cast(@CHARACTER_MAXIMUM_LENGTH as nvarchar(20)),'NULL')+' Where RowIndex=3'
 set @RowIndex=@RowIndex+1
End
set @SQL=@SQL+'
 Select * from @Table'

 exec sp_executesql @SQL