declare @indextable table(id int identity(1,1),nid int)set @sql='insert into @indextable(nid) select ItemId from Info'+right(@where,len(@where)-4)
exec (@sql)
查询分析器提示
必须声明变量 '@indextable'。改成
set @sql='insert into'+ @indextable+'(nid) select ItemId from Info where' +right(@where,len(@where)-4)提示有语法错误,还是必须声明变量 '@indextable'。怎么修改才能执行这条sql语句

解决方案 »

  1.   

    第一次出错是因为:
    DECLARE @s TABLE(...)
    EXEC ('SELECT  * FROM @s')
    EXEC中要执行的语句,与外面的语句编译不在同一空间,执行级别不同,对于EXEC内的语句,@s是个未声明过的变量。第二次出错是因为:
    你的@indextable是个表变量
    'insert into' + @indextable 这种写法是连字串用的,你用字串与表变量连,肯定错了。除非你的@indextable代指一个表名。看你的意思是为了生成一个连续的标识列,用临时表就可以了。
    SELECT itemid,IDENTITY(int) NewI INTO 临时表名 FROM Info WHERE ..