• sql分頁存儲過程


    CREATE PROCEDURE [dbo].[UP_GetRecordByPage]
        @tblName      varchar(255),       --table name
        @fldName      varchar(255),       --key filed name
        @PageSize     int = 10,           --page size
        @PageIndex    int = 1,            --page index
        @IsReCount    bit = 0,            --record count
        @OrderType    bit = 0,            --asc:0 or desc:1
        @strWhere     varchar(1000) = ''  --where
    AS

    declare @strSQL   varchar(6000)      
    declare @strTmp   varchar(100)       
    declare @strOrder varchar(400)       

    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

    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

    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

    if @PageIndex = 1
    begin
        set @strTmp =''
        if @strWhere != ''
            set @strTmp = ' where ' + @strWhere

        set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
            + @tblName + ']' + @strTmp + ' ' + @strOrder
    end

    if @IsReCount != 0
        set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere

    exec (@strSQL)

  • 相关阅读:
    如何消除一个数组里面重复的元素?
    行内元素有哪些?块级元素有哪些? 空(void)元素有那些?
    简述一下src与href的区别
    请说出三种减少页面加载时间的方法
    SQL大全
    11.Hibernate 拦截器
    10.Hibernate 批处理
    9.Hibernate 缓存
    8.Hibernate 原生 SQL
    7.Hibernate 标准查询
  • 原文地址:https://www.cnblogs.com/brawei/p/788721.html
Copyright © 2020-2023  润新知