日期:2014-05-17 浏览次数:20483 次
--表变量是不能进行字符串+运算的.且表变量在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