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

    sp_Pagination

    Create PROCEDURE [dbo].[sp_Pagination]
    @table nvarchar(max), -- 表名称
    @field nvarchar(max)=N'*', -- 字段名称
    @where nvarchar(max)=N' Where 1=1',-- 条件
    @pk nvarchar(20)=N'ID', -- 关键字
    @order nvarchar(max)=N'', -- 排序
    @pSize int=40, -- 页大小
    @pCurrent int = 1 -- 当前页
    AS
    declare @page bigint
    declare @m_exec nvarchar(4000)
    declare @tsql11 nvarchar(4000)
    if(@where is null OR @where='')
    set @where=N' Where 1=1';
    if(@pCurrent <= 1)
    begin
    set @pCurrent=1
    set @m_exec = 'Select TOP ' + CAST(@pSize as varchar(100)) + ' ' + @field + ' From ' + @table + ' ' + @where +' '+ @order
    print @m_exec
    EXECUTE(@m_exec)
    end

    else
    begin
    set @page = @pSize * (@pCurrent - 1)
    -- print @page
    -- 分页的原理是根据它来的->select top 10 * from table1 where name not in(select top 8 name from table1)
    set @m_exec = 'Select TOP ' + CAST(@pSize as varchar(100)) + ' ' + @field + ' From ' + @table + ' ' + @where + ' AND ' + @pk + ' not in(Select TOP ' + CAST(@page as varchar(100)) + ' ' + @pk + ' From ' + @table + ' ' + @where + ' ' + @order + ') ' +' '+ @order
    --print @m_exec
    EXECUTE(@m_exec)
    end


    set @tsql11 = 'Select COUNT(' + @pk + ') as n,CEILING(CAST(COUNT(' + @pk + ') as numeric(10,4))/CAST(' + rtrim(ltrim(str(@pSize))) + ' as numeric(10,4))) as p,' + rtrim(ltrim(str(@pSize))) + ' as size,' + rtrim(ltrim(str(@pCurrent))) + ' as pCur From ' + @table + ' ' + @where
    EXECUTE(@tsql11)

    --print @tsql11

  • 相关阅读:
    nvidia tx1使用记录--基本环境搭建
    STL hashtable阅读记录
    Linux strace命令
    转一篇:Reactor模式
    C++ 模板特化以及Typelist的相关理解
    C++ 内联函数inline
    迭代器失效的几种情况总结
    C++ Class与Struct的区别
    C++中string.find()函数,string.find_first_of函数与string::npos
    C/C++ 中长度为0的数组
  • 原文地址:https://www.cnblogs.com/wei2yi/p/2334826.html
Copyright © 2011-2022 走看看