红包
◆ [2024-3-29 星期五 22:7] ◆ 您来自:54.234.124.70,欢迎您访问風雲工作室。 收藏本站 ◆ | ◆ 设为首页
联系站长(腾讯QQ)
5029111 [風雲]
站长当前离线
首  页 论坛交流 游戏频道 无忧脚本 旧版论坛 云服务器 聊 天 室 自助链接 来访记录 访客留言 搜索提供
位置:風雲工作室 - 论坛交流 - 技术专栏 - 后台开发 - 分页存储过程 返回
主题:分页存储过程
Rimifon
★☆☆☆☆☆☆☆☆☆
积分:285
发帖:317
登录:2024/3/26
注册:2006/6/28
(1楼)分页存储过程
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[SplitPage]
(
    @SelectCommandText nvarchar(4000), -- 要执行的查询命令
    @CurrentPageIndex int = 0,  -- 当前页的索引,从 0 开始
    @PageSize int = 20,  -- 每页的记录数
    @RowCount int = 0 out, -- 总的记录数
    @PageCount int = 0 out -- 总的页数
)
AS

SET NOCOUNT ON

DECLARE @p1 int

SET @CurrentPageIndex = @CurrentPageIndex + 1

EXEC    sp_cursoropen
        @p1 output,
        @SelectCommandText,
        @scrollopt = 1,
        @ccopt = 1,
        @RowCount = @RowCount output;

SELECT @RowCount;

SELECT @PageCount = ceiling(1.0 * @RowCount / @PageSize);

SELECT @CurrentPageIndex = (@CurrentPageIndex - 1) * @PageSize + 1

EXEC    sp_cursorfetch
        @p1,
        16,
        @CurrentPageIndex,
        @PageSize;

EXEC    sp_cursorclose
        @p1
GO


/* 分页存储过程调用方法 */

DECLARE    @return_value int,
        @RowCount int,
        @PageCount int

EXEC    @return_value = [dbo].[SplitPage]
        @SelectCommandText = N'select * from PageTest',
        @CurrentPageIndex = 0,
        @PageSize = 4,
        @RowCount = @RowCount OUTPUT,
        @PageCount = @PageCount OUTPUT

SELECT    @RowCount as N'@RowCount',
        @PageCount as N'@PageCount'

SELECT    'Return Value' = @return_value

GO


时间:2008年6月10日 11:07:46 IP:已记录 引用 回复

© Copyright 2006-2024,風雲工作室 All rights reserved.
湘ICP备05009306号QQ登录
操作 1 个库,连接 2 次,执行 7 次,耗时 219 毫秒。