zoukankan      html  css  js  c++  java
  • 通用SQL分页过程

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go





    ALTER PROCEDURE [dbo].[sp_Select_Page]
    (
        @pageSize int ,                --一页要显示的行数  
        @pageIndex int,                --第几页   参数
        @tabname nvarchar(100),     --对那张表
        @tdname nvarchar(200),        --需要那些字段
        @where nvarchar(2000),        --条件
        @order nvarchar(200),        --排序规则 例如  order by id,name
        @orderd nvarchar(200),        --反排序  例如  order by id desc,name desc
        @recount int=1 output,        --得到查询的总条数
        @pagsum int=1 output        --得到查询的总页数
    )
    AS
    BEGIN
        Declare @sql nvarchar(4000)   --变量
        Declare @sql1 nvarchar(4000)
        set @sql1='select @recount=count(*) from '+@tabname +' '+@where
        exec sp_executesql @sql1,N'@recount int out ',@recount out
        print @recount
        if(@recount%@pageSize)=0
        begin
            set @pagsum=@recount/@pageSize
        end
        else
        begin
            set @pagsum=@recount/@pageSize+1
        end
        print @pagsum
        if((@pageSize*@pageIndex)<@recount)
        begin
            set @sql='select '+ 'top '+str(@pageSize*@pageIndex)+'' +@tdname +' from ' +@tabname +' '+@where+' '+@order
            set @sql1='select '+ 'top '+str(@pageSize)+'' +@tdname +' from ('+@sql +') as a ' +@orderd
            set @sql='select '+ @tdname +' from (' +@sql1+') as b ' +@where+' '+@order
        end
        else
        begin
            set @sql='select '+ 'top '+str(@pageSize*@pageIndex)+'' +@tdname +' from ' +@tabname +' '+@where+' '+@order
            set @sql1='select '+ 'top '+str(@recount-@pageSize*(@pageIndex-1))+'' +@tdname +' from ('+@sql +') as a '+@orderd
            set @sql='select '+ @tdname +' from (' +@sql1+') as b ' +@where+' '+@order
        end
        print @sql
    print 3
        exec sp_executesql @sql
    END

  • 相关阅读:
    页面跳转刷新
    表格表头绘制对角线(不固定表格宽高)
    发送邮件的工具类
    重写equals()和hashCode()
    设计模式--原型模式[转载]
    设计模式--外观模式
    设计模式--代理模式
    js处理json js递归
    MySQL锁详解
    开发一个微信小程序实例教程
  • 原文地址:https://www.cnblogs.com/hznet/p/1805288.html
Copyright © 2011-2022 走看看