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

    sqlserver 单表(视图)通用分页存储过程

    create procedure dbo.proc_getpage
    @table_name varchar(500),              --表名,多表连接:tablea a left join tableb b on a.id=b.fk_id
    @select_fields varchar(1000)=' * ',    --需要返回的列
    @page_size int=10,                     --页尺寸,如果为0则表示返回所有行,不分页
    @page_index int=1,                     --页码
    @sort_field varchar(50)='',            --排序字段 
    @order_type varchar(10)='desc',        --排序类型, asc|desc
    @str_where varchar(max)='',            --查询条件(注意: 不要加 where)
    @row_count int output                  --总记录数
    as
    begin
        declare @select_sql varchar(max)    --主语句
        declare @count_sql nvarchar(max)    --获取总记录数语句
        declare @order_sql varchar(400)     --排序子语句
    
        if(@str_where!='')
        begin
            set @count_sql=N'select @rowcount=count(*) from ' + @table_name + N' where ' + @str_where
        end
        else
        begin
            set @count_sql=N'select @rowcount=count(*) from ' + @table_name
        end
    
        set @order_sql=' order by ' + @sort_field + ' ' + @order_type
    
        if(@page_size=0)
        begin
            if(@str_where!='') 
            begin
                set @select_sql='select ' + @select_fields + ' from ' + @table_name + ' where ' + @str_where + ' ' + @order_sql
            end
            else
            begin
                set @select_sql='select ' + @select_fields + ' from ' + @table_name + ' ' + @order_sql
            end
        end 
        else 
        begin
            set @select_sql='select * from (select row_number() over(' + @order_sql + ') as row_no,' + @select_fields + ' from ' + @table_name
            
            if(@str_where!='')
            begin
                set @select_sql+=' where ' + @str_where
            end
    
            set @select_sql+=') as temp where row_no between ' + cast(((@page_index-1)*@page_size+1) as varchar) + ' and ' + cast((@page_index*@page_size) as varchar)
        end 
        
        exec sp_executesql @count_sql,N'@rowcount int output',@row_count output
        exec(@select_sql)
    end
  • 相关阅读:
    从零开始webpack4.x(五) js处理
    从零开始webpack4.x(四)样式loader
    从零开始webpack4.x(三)html插件
    从零开始webpack4.x(二)基础
    从零开始webpack4.x(一)介绍
    【转】react和vue渲染流程对比
    css3相关
    html5相关
    this指向
    整数划分问题(递归)
  • 原文地址:https://www.cnblogs.com/laoq112/p/11898606.html
Copyright © 2011-2022 走看看