数据库结构如下:
A1 A2
1 b1
2 b2
3 b3
4 b4
. .
19 b19
通过sql将显示结果变为如下
A1 1 2 3 4 ...19
A2 b1 b2 b3 b4...b19
A2字段为时间字段,b19为最后一个时间记录,向前取10条记录
A1 A2
1 b1
2 b2
3 b3
4 b4
. .
19 b19
通过sql将显示结果变为如下
A1 1 2 3 4 ...19
A2 b1 b2 b3 b4...b19
A2字段为时间字段,b19为最后一个时间记录,向前取10条记录
declare @A2 varchar(2000)set @A1 = ''
select @A1=',' + @A1+A1 from tableset @A2 = ''
select @A2=',' + @A2 + convert(varchar(10),A2,121) from tableselect 'A1' + @A1 union
select 'A2' + @A2不一定正確,因為沒有測試
declare @A1 varchar(10),@A2 varchar(10)
declare @strSQL varchar(500),@str1 varchar(250),@str2 varchar(250)set @str1='select ''a1'' as a1,'
set @str2='select ''a2'' as a2,' declare ta scroll cursor
for
select * from test1
open ta fetch first from ta into @AID,@A1,@A2
while @@FETCH_STATUS = 0
BEGIN
set @str1 = @Str1 + ''''+ @A1 +''''+ ' as t' + @A1 + ','
set @str2 = @Str2 + ''''+ @A2 +''''+ ' as t' + @A1 + ','
fetch next from ta into @AID,@A1,@A2
END close ta
deallocate taset @str1 = substring(@str1,1,len(@str1)-1)
set @str2 = substring(@str2,1,len(@str2)-1)--select @str1, @str2select @strSQL = @str1 + ' union '+@str2Exec(@strSQL)
把数据都select出来在进行格式处理把
学习!。