必须声明标量变量 "@indextable"。
declare @indextable table(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) select ID from Info order by ID desc
set @sqlStr=@sqlStr + 'select * from Info O, ' + @indextable +' t where O.ID=t.nid
and t.id between ' + @startIndex + ' and ' + @endIndex
print @sqlStr
请问如上代码,明明已经申明了@indextable ,为什么还报错,要申明标量变量 "@indextable"。呢?
请大家帮帮忙,谢谢了!
[解决办法]
@sqlStr是varchar形式的,@indextable是table形式的,你set @sql=...+@indextable
[解决办法]
你的类型结构不一样,所以回报错
[解决办法]
--表变量是不能进行字符串+运算的.且表变量在EXEC的作用域中是看不见的.
--根据楼主的意图,参考如下:
DECLARE @sqlStr VARCHAR(MAX), @startIndex INT, @endIndex INT
CREATE TABLE #indextable (id int identity(1,1),nid int)
set rowcount @endIndex
insert into #indextable(nid) select ID from Info order by ID DESC
set @sqlStr=@sqlStr + 'select * from Info O, #indextable t where O.ID=t.nid and t.id between ' + LTRIM(@startIndex) + ' and ' + LTRIM(@endIndex)
EXEC(@sqlStr)
create table #indextable(id int identity(1,1),nid int)
set rowcount @endIndex
insert into #indextable(nid)
select ID from Info order by ID desc
set @sqlStr=@sqlStr + 'select * from Info O, ' + '#indextable' +' t where O.ID=t.nid
and t.id between ' + @startIndex + ' and ' + @endIndex
print @sqlStr