读书人

sql语法求教,该怎么处理

发布时间: 2012-03-02 14:40:29 作者: rapoo

sql语法求教
create table test (a int,数量01月 int,数量02月 int,数量03月 int,数量04月 int,数量05月 int,数量06月 int)
insert into test select 1,100,100,100,100,100,100
union select 1,200,200,200,200,200,200
union select 1,300,300,300,300,300,300
union select 1,400,400,400,400,400,400

Declare @I int
select @I=1
--select 数量+Right(100+@I,2)+月 ' from test --(语句一)
select [数量01月] from test --(语句二)
drop table test

问题:要求语句二对应@I
如果:select @I=5
结果对应为:select [数量02月] from test



[解决办法]
Declare @I int
declare @sql nvarchar(4000)
select @I=1
set @sql= 'select 数量 '+Right(100+@I,2)+ '月 from test ' --(语句一)
print @sql
exec(@sql)

读书人网 >SQL Server

热点推荐