34,575
社区成员
发帖
与我相关
我的任务
分享
declare @i int
declare @t int
declare @sql varchar(max)
set @i=1
set @t=120 --t开头的列数
while @i<=@t
begin
set @sql=isnull(@sql+' union all ','')+'select linkid,speeddate,'+'''t'+CAST(@i as varchar)+''' as t,t'+CAST(@i as varchar)+' from table'
set @i=@i+1
end
exec('select * from ('+@sql+') as A order by t')
[/quote]
谢谢各位!!!
declare @i int
declare @t int
declare @sql varchar(max)
set @i=1
set @t=120 --t开头的列数
while @i<=@t
begin
set @sql=isnull(@sql+' union all ','')+'select linkid,speeddate,'+'''t'+CAST(@i as varchar)+''' as t,t'+CAST(@i as varchar)+' from table'
set @i=@i+1
end
exec('select * from ('+@sql+') as A order by t')
[/quote]
print 有长度限制的,但是可以select出来,你试下就知道了
DECLARE @i INT
DECLARE @sql NVARCHAR(MAX)
SET @i=1
SET @sql='select linkid,speeddate,t1 t from table'
WHILE @i<=94
BEGIN
SET @i=@i+1
SET @sql=@sql+N'
union all
select linkid,speeddate,t'+CAST(@i AS NVARCHAR(2))+' t from table'
END
SET @sql=@sql+' order by linkid'
PRINT @sql
SELECT @sql[/quote]
非常好用 大神 非常感谢!!
declare @i int
declare @t int
declare @sql varchar(max)
set @i=1
set @t=120 --t开头的列数
while @i<=@t
begin
set @sql=isnull(@sql+' union all ','')+'select linkid,speeddate,'+'''t'+CAST(@i as varchar)+''' as t,t'+CAST(@i as varchar)+' from table'
set @i=@i+1
end
exec('select * from ('+@sql+') as A order by t')
[/quote]
print 有长度限制的,但是可以select出来,你试下就知道了
DECLARE @i INT
DECLARE @sql NVARCHAR(MAX)
SET @i=1
SET @sql='select linkid,speeddate,t1 t from table'
WHILE @i<=94
BEGIN
SET @i=@i+1
SET @sql=@sql+N'
union all
select linkid,speeddate,t'+CAST(@i AS NVARCHAR(2))+' t from table'
END
SET @sql=@sql+' order by linkid'
PRINT @sql
SELECT @sql
declare @i int
declare @t int
declare @sql varchar(max)
set @i=1
set @t=120 --t开头的列数
while @i<=@t
begin
set @sql=isnull(@sql+' union all ','')+'select linkid,speeddate,'+'''t'+CAST(@i as varchar)+''' as t,t'+CAST(@i as varchar)+' from table'
set @i=@i+1
end
exec('select * from ('+@sql+') as A order by t')
select linkid,speeddate,'t1' as t,t1 from table
union all
select linkid,speeddate,'t2' as t,t2 from table
union all
select linkid,speeddate,'t3' as t,t2 from table
.
.
.
union all
select linkid,speeddate,'tn' as t,tn from table