• SQL 2005 多表分页存储过程


    更多分页请访问: http://www.520teacher.com/

    **********************************************************

    #region 返回所有职位对象 带分页

            /// <summary>
            /// #region 返回所有职位对象带分页
            /// </summary>
            /// <param name="curPage">当前页</param>
            /// <param name="size">每页显示数</param>
            /// <param name="pageSize">返回 页数</param>
            /// <param name="totalSize">返回总项数</param>
            /// <returns><对象集合></returns>

            public static List<PostionModel> GetALlModel(int curPage, int size, ref int pageSize, ref  int totalSize)
            {
                List<PostionModel> PostionModelList = new List<PostionModel>();

                SqlDataReader tmpDR = null;

                PostionModel tmpModel = null;

                //字段名
                SqlParameter strFields = new SqlParameter("@strFields", SqlDbType.NVarChar, 500);
                strFields.Value = "Department.departmentName,Positions.ID,Positions.Positionname";

                //表名
                SqlParameter strTableName = new SqlParameter("@strTableName", SqlDbType.NVarChar, 500);
                strTableName.Value = "Department,Positions";

                //条件 无需加where
                SqlParameter strWhere = new SqlParameter("@strWhere", SqlDbType.NVarChar, 500);
                strWhere.Value = " Positions.departmentid = Department.id  ";


                //排序 必添 无需加order by
                SqlParameter strOrderBy = new SqlParameter("@strOrderBy ", SqlDbType.NVarChar,200);
                strOrderBy.Value = " Department.id , Positions.ID ";
              

                //分页大小
                SqlParameter PageSize = new SqlParameter("@PageSize", SqlDbType.Int);
                PageSize.Value = size;

                //当前页 1为起始页
                SqlParameter CurrentPage = new SqlParameter("@CurrentPage", SqlDbType.Int);
                CurrentPage.Value = curPage > 0 ? curPage : 1;

                //返回总页数
                SqlParameter PageCount = new SqlParameter("@PageCount", SqlDbType.Int);
                PageCount.Direction = ParameterDirection.Output;

                //返回记录总数
                SqlParameter RecordCount = new SqlParameter("@RecordCount", SqlDbType.Int);
                RecordCount.Direction = ParameterDirection.Output;

                try
                {


                    using (tmpDR = SQLHelper.GetReader("sp_AbiTableLoad", strFields, strTableName, strWhere, strOrderBy, PageSize, CurrentPage, PageCount, RecordCount))
                    {

                        if (tmpDR != null && !tmpDR.IsClosed)
                        {
                            while (tmpDR.Read())
                            {
                                tmpModel = new PostionModel();


                                //TaskID,TaskName,realName,IsNew,IsShow,IsAccept,AcceptDate,AddDate,PublistManName


                                tmpModel.ID = DBNull.Value.Equals(tmpDR["ID"]) ? 0 : (int)tmpDR["ID"];

                                tmpModel.RootName = DBNull.Value.Equals(tmpDR["departmentName"]) ? "不存在" : tmpDR["departmentName"].ToString();

                                tmpModel.PositionName = DBNull.Value.Equals(tmpDR["Positionname"]) ? "不存在" : tmpDR["Positionname"].ToString();


                                PostionModelList.Add(tmpModel);

                            }
                        }
                    }
                }
                catch (Exception EX)
                {

                    SendMail.SendMails("GetPostionModelTel() error", EX.Message + EX.Source, "", "system@norco.com.cn");
                }
                finally
                {
                    if (tmpDR != null && !tmpDR.IsClosed)
                    {

                        tmpDR.Close();
                        tmpDR.Dispose();
                    }
                }


                pageSize = (int)PageCount.Value;

                totalSize = (int)RecordCount.Value;


                return PostionModelList;
            }

            #endregion

     ************************************************************

    测试: 排序项不能为空

    declare @i int
    declare @j int

    exec sp_AbiTableLoad
    'Department.departmentName,Positions.ID,Positions.Positionname',
    'Department,Positions',
    ' Positions.departmentid = Department.id ',
    'Positions.ID',
    2,
    5,
    @i output,
    @j output

    select @i,@j

    Create proc [dbo].[sp_AbiTableLoad]
    ----------------------------------------------
    --        单表多表分页存储过程    --
    -- 1.支持单表多表分页查询     --
    -- 2.支持排序        --
    -- 3.支持表名重命名       --
    -- 4.返回总页数        --
    -- 5.返回总记录数       --
    -- 6.带行编号        --
    -- 缺陷:         --
    -- 1.多表查询时,各表中各列名不许出现重名 --
    -- Edit by Shardine 2007.1.19    --
    ----------------------------------------------
    @strFields varchar(500),  --字段名
    @strTableName varchar(500), --表名
    @strWhere varchar(500),  --条件 无需加where
    @strOrderBy varchar(200), --排序 必添 无需加order by
    @PageSize int,    --分页大小
    @CurrentPage int,   --当前页,1为起始页
    @PageCount int output,  --返回总页数
    @RecordCount int output  --返回记录总数
    as
    begin
    declare @StartIndex int     --定义起始位置
    set @StartIndex = (@currentPage - 1) * @PageSize + 1
    declare @strSql1 nvarchar (800) --数据查询
    declare @strSql2 nvarchar (1000) --统计记录总数
    declare @ParmDefinition nvarchar (800)
    set @ParmDefinition = N'@tmp int output'
    set @strSql1 = N'select row_number() over (order by ' + @strOrderBy + ' ) as RowID, '
    set @strSql2 = 'select @tmp = count(*) '
    if @strFields <> ''
     set @strSql1 = @strSql1 + @strFields
    else
     set @strSql1 = @strSql1 + ' * '
    if @strTableName <> ''
    begin
     set @strSql1 = @strSql1 + ' from ' + @strTableName
     set @strSql2 = @strSql2 + ' from ' + @strTableName
    end
    if @strWhere <> ''
    begin
     set @strSql1 = @strSql1 + ' where ' + @strWhere
     set @strSql2 = @strSql2 + ' where ' + @strWhere
    end
    exec sp_executesql @strSql2,@ParmDefinition,@tmp = @RecordCount output  --执行统计记录总数SQL语句

    if @RecordCount % @PageSize = 0  --计算总页数
     set @PageCount = @RecordCount / @PageSize
    else
     set @PageCount = @RecordCount / @PageSize + 1
    set @strSql1 = 'with TempTable as ( ' + @strSql1 + ' ) select * from TempTable where RowID between '
      + Convert(varchar(10),@StartIndex) + ' and ' + Convert(varchar(10),@StartIndex + @PageSize - 1)
    exec(@strSql1)
    end

    更多分页请访问: http://www.520teacher.com/
  • 相关阅读:
    SVN补充
    java面试资料总结
    JAVABEAN EJB POJO区别
    Hibernate使用原生sql语句
    Discrete Logging(poj 2417)
    卡牌游戏(bzoj 3191)
    Activation(hdu 4089)
    Aeroplane chess(hdu 4405)
    LOOPS(hdu 3853)
    巧克力(zoj 1363)
  • 原文地址:https://www.cnblogs.com/tangself/p/1727844.html
Copyright © 2020-2023  润新知