问题是这样的:
变量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;
很简单的。
1 exec('select '+变量1 + ' from td')
动态拼接sql吗
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