当前位置: 代码迷 >> Sql Server >> 疑难杂症就高手啊解决方案
  详细解决方案

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

热度:68   发布时间:2016-04-27 18:44:26.0
疑难杂症就高手啊
列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 [email protected]create table #Cube(Row int not null)while @Row<[email protected]begin    insert #Cube values(@Row)    select @[email protected]+1enddeclare @SQL nvarchar(4000)select @SQL='select 'while @Col<@ColCountbegin    select @[email protected]+'(case when #Temp.col='+cast(@Col as varchar)+ 'then #Temp.Value end) as COL'+cast(@Col as varchar)+','    select @[email protected]+1end    select @[email protected]+'(case when #Temp.col='+cast(@Col as varchar)+ 'then #Temp.Value end) as COL'+cast(@Col as varchar)    select @[email protected]+'
  相关解决方案