标题: SQL Server通用分页存储过程:利用SQL Server未公开的存储过程实现
ljjk5
元帅
Rank: 1


荣誉会员奖章
UID 46706
精华 1
积分 99426
帖子 49691
威望 554
金币 48489
热心 505
阅读权限 100
注册 2007-2-25
状态 离线
SQL Server通用分页存储过程:利用SQL Server未公开的存储过程实现

存储过程定义:
/**//****** 对象:  StoredProcedure [dbo].[SplitPage]    脚本日期: 04/23/2007 16:10:08 ******/
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
调用方法:
DECLARE    @return_value int,
        @RowCount int,
        @PageCount int
EXEC    @return_value = [dbo].[SplitPage]
        @SelectCommandText = N'SELECT * FROM Log',
        @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

网友 ljjk5 签名 - 网友社区 ===
顶部
[广告] 免费域名(Free Subdomain) 免费空间(Free hosting) PR查询(Google Pagerank)



当前时区 GMT+8, 现在时间是 2008-9-8 12:47
信产部ICP备案:京ICP备05066424号 北京市公安局网监备案:1101050648号

Powered by Discuz! 5.5.0
清除 Cookies - 联系我们 - 网友俱乐部 - Archiver - WAP