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

    if exists(select * from sys.objects where name='存储过程名称')
    drop proc 存储过程名称
    go
    CREATE proc 存储过程名称
    @tableName varchar(8000),          --表名、视图名
    @indexCol varchar(50) = 'a.id',      --标识列名(如:比如主键、标识,推荐使用索引列)
    @pageSize int = 10,                --页面大小
    @pageIndex int = 0,                --当前页
    @orderCol varchar(100) = 'a.id desc',--排序 (如:id)
    @where varchar(max) = '',         --条件
    @columns varchar(500) = '*'        --要显示的列
    as
    declare @sql varchar(max)
    declare @sql2 varchar(max)
    declare @where2 varchar(max)

    if @where <> ''
    begin
        select @where2 = ' And ' + @where
        select @where = ' Where ' + @where
    end
    else
        select @where2 = ''


    select @sql = 'Select Top ' + Convert(varchar(10),@pageSize) + ' ' + @columns + ' From ' + @tableName
    select @sql2 = @sql + @where
    select @sql =  @sql + ' Where ' + '(' + @indexCol + ' Not In (Select Top ' + Convert(varchar(10),  ((@pageIndex-1)*@pageSize)) + ' ' + @indexCol + ' From ' + @tableName + @where +  ' Order by '+ @orderCol +'))'
    select @sql = @sql + @where2
    select @sql = @sql + ' Order by ' + @orderCol
    --获取数据集
    exec (@sql)
    PRINT @sql
    select @sql2 = Replace(@sql2,'Top ' + Convert(varchar(10), @pageSize) + ' ' + @columns, 'count(1)')
    --获取总数据条数
    exec(@sql2)

    GO

  • 相关阅读:
    使用_Capistrano_进行自动化部署(2)
    使用 Capistrano 进行自动化部署
    Zend_Framework_1 框架是如何被启动的?
    PHP新版本变化
    Phpstorm 无法自动断点 Exception
    理解希尔排序
    C# Thread 线程
    Unity 依赖注入容器的AOP扩展
    C# 面向切面编程 AOP
    C# 表达式树 Expression
  • 原文地址:https://www.cnblogs.com/zcc0912/p/9388347.html
Copyright © 2011-2022 走看看