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)

  • 相关阅读:
    JQury RadioButton
    Oracle 错误
    C#基础 ASP.NET基本技巧
    后台管理系统框架模版(静态页)
    复习串讲 day02:搭建邮件服务、空客户端、搭建数据库服务、数据库基础操作
    ENGINEER 004:Linux命令字的来源、alias别名设置
    ENGINEER 003:配置IPv6地址
    ENGINEER 002: 配置用户环境 --- 配置聚合连接
    ENGINEER 001:SELinux安全机制
    命令行基础
  • 原文地址:https://www.cnblogs.com/brawei/p/788721.html
Copyright © 2011-2022 走看看