MSSQL循环添加列
--这个表中放入ID,与我将要添加的列名select identity (int,1,1)as ID ,HW_PART_NO into HW_Row from ['WINDOWS 7 32$'] group by HW_PART_NOdeclare @row nvarchar(255)declare @i intselect @i=COUNT(*)from SUB_Rowwhile(@i>0)beginselect @row=SUB_PH_NUMBER_AND_DESCRIPTION from SUB_Row where SUBID=@iset @i=@i-1alter table SRV_SUB add @row nvarchar(255)end报错:Msg 102, Level 15, State 1, Line 8Incorrect syntax near '@row'.
--这个表中放入ID,与我将要添加的列名select identity (int,1,1)as ID ,HW_PART_NO into HW_Row from ['WINDOWS 7 32$'] group by HW_PART_NOdeclare @row nvarchar(255)declare @i intselect @i=COUNT(*)from SUB_Rowwhile(@i>0)beginselect @row=SUB_PH_NUMBER_AND_DESCRIPTION from SUB_Row where SUBID=@iset @i=@i-1EXEC ('alter table SRV_SUB add '+@ROW+' nvarchar(255)')end
[解决办法]
select identity (int,1,1)as ID ,HW_PART_NO into HW_Row from ['WINDOWS 7 32$'] group by HW_PART_NOdeclare @row nvarchar(255)declare @i intselect @i=COUNT(*)from SUB_Rowwhile(@i>0)beginselect @row=SUB_PH_NUMBER_AND_DESCRIPTION from SUB_Row where SUBID=@iset @i=@i-1exec('alter table SRV_SUB add '+@row+' nvarchar(255)')end
[解决办法]
try this,
alter table SRV_SUB add @row nvarchar(255)--> 改为exec('alter table SRV_SUB add ['+@row+'] nvarchar(255) ')
[解决办法]
EXEC ('alter table SRV_SUB add '+@ROW+' nvarchar(255)')
[解决办法]
DDL语句不支持带变量.