分页存储过程

Posted on 2008-08-26 18:01  Yruigood  阅读(272)  评论(0编辑  收藏  举报

新人开博,先来个存储过程。

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE  [YR_DataPaging]
@sql NVARCHAR(4000),    --查询字符串
@currentpage INT = 1,   --当前页
@pagesize INT = 10,     --每页行数
@pagecount INT = 0 OUT, --总页数
@totalcount INT = 0 OUT --总记录数
AS
SET NOCOUNT ON
DECLARE @p1 INT         --P1是游标的id
EXEC sp_cursoropen @p1 OUTPUT, @sql, @scrollopt = 1, @ccopt=1, @rowcount = @pagecount OUTPUT
SELECT @totalcount = @pagecount
, @pagecount = CEILING(1.0 * @pagecount / @pagesize)
, @currentpage = (@currentpage - 1) * @pagesize + 1
EXEC sp_cursorfetch @p1, 16, @currentpage, @pagesize
EXEC sp_cursorclose @p1

Copyright © 2024 Yruigood
Powered by .NET 9.0 on Kubernetes