SQLSERVER Pager store procedure

SQL SERVER(2005)以上版本可用

 

代码

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[Pagination]
@Page int = 1, -- 当前页码
@PageSize int = 10, -- 每页记录条数(页面大小)
@Table nvarchar(500), -- 表名或视图名,甚至可以是嵌套SQL:(Select * From Tab Where ID>1000) Tab
@Field nvarchar(800) = '*', -- 返回记录集字段名,","隔开,默认是"*"
@OrderBy nvarchar(100) = 'ID ASC', -- 排序规则
@Filter nvarchar(500), -- 过滤条件
@MaxPage smallint output, -- 执行结果 -1 error, 0 false, maxpage true
@TotalRow int output, -- 记录总数 /* 2007-07-12 22:11:00 update */
@Descript varchar(100) output -- 结果描述
AS
BEGIN

-- =============================================
--
Author: Jimmy.Yu
--
Create date: 2007-5-11
--
Description: SQL 2005 以上版本 通用分页存储过程
--
=============================================

Set ROWCOUNT @PageSize;

Set @Descript = 'successful';
-------------------参数检测----------------
IF LEN(RTRIM(LTRIM(@Table))) !> 0
Begin
Set @MaxPage = 0;
Set @Descript = 'table name is empty';
Return;
End

IF LEN(RTRIM(LTRIM(@OrderBy))) !> 0
Begin
Set @MaxPage = 0;
Set @Descript = 'order is empty';
Return;
End

IF ISNULL(@PageSize,0) <= 0
Begin
Set @MaxPage = 0;
Set @Descript = 'page size error';
Return;
End

IF ISNULL(@Page,0) <= 0
Begin
Set @MaxPage = 0;
Set @Descript = 'page error';
Return;
End
-------------------检测结束----------------

Begin Try
-- 整合SQL
Declare @SQL nvarchar(4000), @Portion nvarchar(4000);

Set @Portion = ' ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') AS ROWNUM FROM ' + @Table;

Set @Portion = @Portion + (CASE WHEN LEN(@Filter) >= 1 THEN (' Where ' + @Filter + ') AS tab') ELSE (') AS tab') END);

Set @SQL = 'Select TOP(' + CAST(@PageSize AS nvarchar(8)) + ') ' + @Field + ' FROM (Select ' + @Field + ',' + @Portion;

Set @SQL = @SQL + ' Where tab.ROWNUM > ' + CAST((@Page-1)*@PageSize AS nvarchar(8));

-- 执行SQL, 取当前页记录集
Execute(@SQL);
--------------------------------------------------------------------

-- 整合SQL
Set @SQL = 'Set @Rows = (Select MAX(ROWNUM) FROM (Select' + @Portion + ')';

-- 执行SQL, 取最大页码
Execute sp_executesql @SQL, N'@Rows int output', @TotalRow output;
Set @MaxPage = (CASE WHEN (@TotalRow % @PageSize)<>0 THEN (@TotalRow / @PageSize + 1) ELSE (@TotalRow / @PageSize) END);
End Try
Begin Catch
-- 捕捉错误
Set @MaxPage = -1;
Set @Descript = 'error line: ' + CAST(ERROR_LINE() AS varchar(8)) + ', error number: ' + CAST(ERROR_NUMBER() AS varchar(8)) + ', error message: ' + ERROR_MESSAGE();
Return;
End Catch;

-- 执行成功
Return;
END

相对应的页面逻辑中写的对应调用该存储过程的方法(C#)

 

protected static DataTable GetPagedData(string sourceTable, int pageIndex, int pageSize, string sort, string filter, string fieldList, out int totalRecords)
        {
            /*
            @Page int = 1,      -- 当前页码
            @PageSize int = 10,     -- 每页记录条数(页面大小)
            @Table nvarchar(500),    -- 表名或视图名,甚至可以是嵌套SQL:(Select * From Tab Where ID>1000) Tab
            @Field nvarchar(200) = '*',   -- 返回记录集字段名,","隔开,默认是"*"
            @OrderBy nvarchar(100) = 'ID ASC', -- 排序规则
            @Filter nvarchar(500),    -- 过滤条件
            @MaxPage smallint output,   -- 执行结果 -1 error, 0 false, maxpage true
            @TotalRow int output,    -- 记录总数  --2007-07-12 22:11:00 update 
            @Descript varchar(100) output  -- 结果描述
             */
            string spPage = "Pagination";

            int maxPage = 0;
            SqlParameter parMaxPage = new SqlParameter("@MaxPage", maxPage);
            parMaxPage.Direction = ParameterDirection.Output;


            SqlParameter parTotalRow = new SqlParameter("@TotalRow", 0);
            parTotalRow.Direction = ParameterDirection.Output;

            string descr = "";
            SqlParameter parDescr = new SqlParameter("@Descript", descr);
            parDescr.Direction = ParameterDirection.Output;

            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@Page",pageIndex),
                new SqlParameter("@PageSize",pageSize),
                new SqlParameter("@Table",sourceTable),
                new SqlParameter("@Field",fieldList),
                new SqlParameter("@OrderBy",sort),
                new SqlParameter("@Filter",filter),
                parMaxPage,
                parTotalRow,
                parDescr
            };



            DataSet ds = SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, spPage, paras);
            totalRecords = Util.ConvertTo<int>(parTotalRow.Value, 0);
            if (ds.Tables.Count > 0)
                return ds.Tables[0];
            else
                return null;
        }

posted on 2010-06-27 14:27  tneduts  阅读(279)  评论(1编辑  收藏  举报

导航