首页新闻找找看学习计划

sql server 变量1的值为变量2的名称,如何通过变量1得到变量2的值

0
悬赏园豆:100 [待解决问题]

问题是这样的:

变量1=循环拼接出来的  @AAA+1;

                               @AAA+2等。

如何将变量1的值拿到,再查询,得到@AAA+1的值?

问题补充:

DECLARE aMy_Cursor CURSOR 

FOR (select strMaterial,intCountTime1,intCountTime2,intCountTime3,intCountTime4,intCountTime5,
intCountTime6,intCountTime7,intCountTime8,intCountTime9,intCountTime10,intCountTime11,intCountTime12,
intCountTime13,intCountTime14
from s_reportadu )
OPEN aMy_Cursor;
FETCH NEXT FROM aMy_Cursor into @strMaterial,@intCountTime1,@intCountTime2,@intCountTime3,
@intCountTime4,@intCountTime5,@intCountTime6,@intCountTime7,@intCountTime8,@intCountTime9,
@intCountTime10,@intCountTime11,@intCountTime12,@intCountTime13,@intCountTime14;
WHILE @@FETCH_STATUS = 0
BEGIN

set @aa=2
while @aa<=@nn+1
begin

set @intCountTime='@intCountTime'+CONVERT(varchar(50),@aa)


exec('insert into T_AduForecast_record
select '''+@strMaterial+''',

???这里根据变量@intCountTime值,得到对应的变量对应的值???,

''Adu_intCountTime''+'''+@aa+''',getdate() ')


set @aa=@aa+1
end

FETCH NEXT FROM aMy_Cursor into @strMaterial,@intCountTime1,@intCountTime2,@intCountTime3,
@intCountTime4,@intCountTime5,@intCountTime6,@intCountTime7,@intCountTime8,@intCountTime9,
@intCountTime10,@intCountTime11,@intCountTime12,@intCountTime13,@intCountTime14;
END
CLOSE aMy_Cursor;
DEALLOCATE aMy_Cursor;

gipiggy的主页 gipiggy | 初学一级 | 园豆:102
提问于:2017-08-24 14:12
< >
分享
所有回答(3)
1

很简单的。

 1 exec('select '+变量1 + ' from td') 

【行止坐卧丶皆修行】 | 园豆:204 (菜鸟二级) | 2017-08-24 17:29
0

动态拼接sql吗

Gamain | 园豆:357 (菜鸟二级) | 2017-08-28 18:57
0

DECLARE @AAA INT

   SET @AAA=1

DECLARE @Index INT

   SET @Index=1

WHILE @Index < 10

BEGIN

   SET @AAA=@AAA+1

   SET @Index=@Index+1

   PRINT(@AAA)

END

 

手工打的,给分吧?哈哈,Index类似for循环的i

chengeng | 园豆:292 (菜鸟二级) | 2017-08-29 16:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册