读书人

疑难杂症就高手啊解决方案

发布时间: 2012-02-22 19:36:55 作者: rapoo

疑难杂症就高手啊
列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 #Tempdeclare @RowCount  int declare @ColCount intdeclare @Row intdeclare @Col intdeclare @Value char(1)select @Value='A'select @Row=1select @Col=1select @RowCount=max(row),@ColCount=max(col) from #Temp where ID=@Valuecreate table #Cube(Row int not null)while @Row<=@RowCountbegin    insert #Cube values(@Row)    select @Row=@Row+1enddeclare @SQL nvarchar(4000)select @SQL='select 'while @Col<@ColCountbegin    select @SQL=@SQl+'(case when #Temp.col='+cast(@Col as varchar)+ 'then #Temp.Value end) as COL'+cast(@Col as varchar)+','    select @Col=@Col+1end    select @SQL=@SQl+'(case when #Temp.col='+cast(@Col as varchar)+ 'then #Temp.Value end) as COL'+cast(@Col as varchar)    select @SQL=@SQl+' 

读书人网 >SQL Server

热点推荐