34,587
社区成员
发帖
与我相关
我的任务
分享
create table tab0705
(jog_id varchar(5),
[1995] int,
[1996] int,
[1997] int,
[1998] int,
[1999] int,
[2000] int)
insert into tab0705
select 'AA',1,2,3,4,5,6 union all
select 'BB',11,22,33,44,55,66 union all
select 'CC',111,222,333,444,555,666
declare @y int,@tsql varchar(2000)
select @y=1998 --> 输入参数
select @tsql=isnull(@tsql+',','select jog_id, ')+'['+b.name+']'
from sys.tables a
inner join sys.columns b on a.object_id=b.object_id
where a.name='tab0705'
and isnumeric(b.name)=1
and b.name<>'jog_id'
and cast(b.name as int)>=@y
select @tsql=@tsql+' from tab0705 '
exec(@tsql)
/*
jog_id 1998 1999 2000
------ ----------- ----------- -----------
AA 4 5 6
BB 44 55 66
CC 444 555 666
(3 row(s) affected)
*/