• MSSQL按分页前去查询成绩的存储历程


      滥觞:网海拾贝




    改一下,看看如许是不是更好一点?
    /*
    函数称谓: GetRecordFromPage
    函数屈服: 获取指定页的数据
    参数声名: @tblName 包罗数据的表名
    @fldName 环节字段名
    @PageSize 每页纪录数
    @PageIndex 要获取的页码
    @IsCount 能否要取得纪录数
    @OrderType 排序模范, 0 - 升序, 1 - 降序
    @strWhere 查询条件 (注意: 不要加 where)
    */
    CREATE PROCEDURE pGO_GetRecordFromPage
    @tblName varchar(255), -- 表名
    @fldName varchar(255), -- 字段名
    @PageSize int = 10, -- 页尺寸
    @PageIndex int = 1, -- 页码
    @IsCount bit = 0, -- 前去纪录总数, 非 0 值则前去
    @OrderType bit = 0, -- 设置排序模范, 非 0 值则降序
    @strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
    AS

    declare @strSQL varchar(6000) -- 主语句
    declare @strTmp varchar(500) -- 姑且变量
    declare @strOrder varchar(400) -- 排序模范

    -- 假设是查询纪录总数,直接运用Count(0)函数
    if @IsCount != 0
    begin
    if @strWhere != ''
    set @strSQL = 'select count(*) as Total from [' @tblName '] where ' @strWhere
    else
    set @strSQL = 'select count(*) as Total from [' @tblName '] '
    end
    --假设是想查询纪录,则
    else
    begin
    if @PageIndex = 1
    begin
    set @strTmp = ''
    if @strWhere != ''
    set @strTmp = ' where ' @strWhere

    set @strSQL = 'select top ' str(@PageSize) ' * from ['
    @tblName ']' @strTmp ' ' @strOrder
    end
    else
    begin
    --假设是降序查询……
    if @OrderType != 0
    begin
    set @strTmp = '<(select min'
    set @strOrder = ' order by [' @fldName '] desc'
    end
    --假设是升序查询……
    else
    begin
    set @strTmp = '>(select max'
    set @strOrder = ' order by [' @fldName '] asc'
    end

    if @strWhere != ''
    set @strSQL = 'select top ' str(@PageSize) ' * from ['
    @tblName '] where [' @fldName ']' @strTmp '(['
    @fldName ']) from (select top ' str((@PageIndex-1)*@PageSize) ' ['
    @fldName '] from [' @tblName '] where ' @strWhere ' '
    @strOrder ') as tblTmp) and ' @strWhere ' ' @strOrder
    else
    set @strSQL = 'select top ' str(@PageSize) ' * from ['
    @tblName '] where [' @fldName ']' @strTmp '(['
    @fldName ']) from (select top ' str((@PageIndex-1)*@PageSize) ' ['
    @fldName '] from [' @tblName ']' @strOrder ') as tblTmp)'
    @strOrder


    end
    end

    exec (@strSQL)
    GO



    版权声明: 原创作品,允许转载,转载时请务必以超链接体式格局标明文章 原始因由 、作者信息和本声明。否则将清查法令责任。

  • 相关阅读:
    python-excel操作
    python-处理文件
    python-pandas应用总结
    python比较数组
    vue学习一(指令1.v-text,v-html,插值表达式{{msg}})
    博客园背景图页面定制css
    SpringBoot的yml文件报org.yaml.snakeyaml.scanner.ScannerException: mapping values are not allowed here in 'reader', line 11, column 16:
    python初学习一
    C#多线程
    API与WebApi
  • 原文地址:https://www.cnblogs.com/zgqjymx/p/1976717.html
Copyright © 2020-2023  润新知