zoukankan      html  css  js  c++  java
  • 存储过程分页语句

    create proc [dbo].[ceb_Pagination]
    @strFields nvarchar(2000),  --字段名
    @strTableName nvarchar(2000), --表名
    @strWhere nvarchar(4000),  --条件 无需加where
    @strOrderBy nvarchar(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 (Max) --数据查询
    declare @strSql2 nvarchar (Max) --统计记录总数
    declare @ParmDefinition nvarchar (Max)
    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)
    SET @strSql1 =@strSql1 +' order by RowID'
    exec(@strSql1)
  • 相关阅读:
    Vue模板
    一个人的旅行
    o2o家庭助手demo
    学习html5 app项目开发
    我最近的一段时间技术总结
    我最近的工作、生活状态
    swift学习初步(四)-- 函数
    swift学习初步(三)--控制流操作
    swift学习(二)--基本运算符、字符串、集合操作
    Swift学习初步(一)
  • 原文地址:https://www.cnblogs.com/dashi/p/3844675.html
Copyright © 2011-2022 走看看