ALTER PROCEDURE [dbo].[Proc_SentPage]
@pageindex int,
@pagesize int,
@TableName varchar(80),
@viewfield varchar(500),
@where varchar(200),
@allcount INT output
AS
BEGIN
CREATE TABLE #tempTable /**创建临时表**/
(
IndexId INT IDENTITY (1,1) NOT NULL,
ID int
)
begin
exec('insert into #tempTable (id) select id from '+@TableName+' '+@where)
end
select @allcount=(select count(*) from #tempTable)
set @viewfield=replace(@viewfield,',',',a.')
set @viewfield='a.'+@viewfield
exec('select '+@viewfield+' FROM '+@TableName+' a INNER JOIN #tempTable ON a.ID = #tempTable.ID
where indexID>'+@pageSize+'*('+@pageIndex+'-1)' +'AND indexID<='+@pageSize+'*'+@pageIndex )
--释放临时表
DROP TABLE #tempTable
END
本文介绍了一种使用SQL存储过程实现动态分页查询的方法。该方法通过创建临时表存储查询结果,并利用输入参数控制返回的数据范围,适用于需要对大量数据进行高效分页展示的应用场景。
8052

被折叠的 条评论
为什么被折叠?



