请教想用循环建表字段,但不知道为什么加字段语句不能用变量?附代码
DECLARE @i int, @num int,@strp char(6)
SET @i = 0
SET @num = 216
WHILE @i < @num
BEGIN
set @strp= 'iii '+char(@i)
ALTER TABLE map ADD @strp decimal NULL
SET @i = @i + 1
END
------解决方案--------------------EXEC( 'ALTER TABLE map ADD '+@strp+ ' decimal NULL ')
要这样
------解决方案-------------------- DECLARE @i int, @num int,@strp char(6)
SET @i = 0
SET @num = 216
WHILE @i < @num
BEGIN
set @strp= 'iii '+rtrim(@i)
exec( 'ALTER TABLE map ADD '+@strp+ ' decimal NULL ')
SET @i = @i + 1
END