zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    安装Apache提示APR not found的解决办法
    使用jQuery和CSS3实现一个数字时钟
    nodejs iconfont处理
    ios html5 长按复制文本
    Weex 开发入门
    Nginx比SRS做得好的地方
    NodeJs mysql 开启事务
    NodeJs使用Mysql模块实现事务处理
    centos7之系统优化方案
    CentOS 7 网络优化(升级内核、开启 BBR)
  • 原文地址:https://www.cnblogs.com/brawei/p/788721.html
Copyright © 2011-2022 走看看