日期:2014-05-18  浏览次数:20426 次

疑难杂症就高手啊
列1 列 2 列3 ....无限列
行1 5
行2 4
行3 5 6  
无限行  

类似于一个excel网格,我存放的界面是如此,而我将这个界面的数据按纵横列,将该数据保存在一个固定表中的一个字段中,比如A值 列1行1:5/列2行2:4/列2行3:5/列3行3:6
将这界面的所有值全部保存到A值中,并且这么显示。。

而我现在想读取该固定表中的A值,并且排布也 上面的行列排列。有什么方法没呢?



------解决方案--------------------
应该是你要的了

SQL code
create table #Temp
( ID char(1) not null, 
  row int not null,
  col int not null,
  Value int not null 
)

insert #Temp values('A',1,1,5)
insert #Temp values('A',2,2,4)
insert #Temp values('A',8,8,6)


select * from #Temp

declare @RowCount  int 
declare @ColCount int
declare @Row int
declare @Col int
declare @Value char(1)
select @Value='A'
select @Row=1
select @Col=1
select @RowCount=max(row),@ColCount=max(col) from #Temp where ID=@Value

create table #Cube(Row int not null)
while @Row<=@RowCount
begin
    insert #Cube values(@Row)
    select @Row=@Row+1
end


declare @SQL nvarchar(4000)
select @SQL='select '
while @Col<@ColCount
begin

    select @SQL=@SQl+'(case when #Temp.col='+cast(@Col as varchar)+ 'then #Temp.Value end) as COL'+cast(@Col as varchar)+','
    select @Col=@Col+1
end
    select @SQL=@SQl+'(case when #Temp.col='+cast(@Col as varchar)+ 'then #Temp.Value end) as COL'+cast(@Col as varchar)
    select @SQL=@SQl+'