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

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go


    ALTER        PROCEDURE [dbo].[GetDataSet] 
    @TableList Varchar(200)='*',
    @TableName Varchar(300),
    @SelectWhere Varchar(1000)='',
    @SelectOrderId Varchar(200),
    @SelectOrder Varchar(200)='',
    @intPageNo int=1--页号
    @intPageSize int=10 --每页显示数
    as
    begin
        
    set nocount on--关闭计数
        declare @RecordCount int  --总记录数(存储过程输出参数)
        declare @TmpSelect      NVarchar(2000)  
        
    declare @Tmp     NVarchar(600)  

        
    if @SelectWhere=''
            
    begin
                
    set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName
            
    end
        
    else
            
    begin
                
    set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName +' where '+@SelectWhere
            
    end
        
    execute sp_executesql  @TmpSelect,    --执行上面的sql语句
        N'@RecordCount int OUTPUT' ,  --执行输出数据的sql语句,output出总记录数
        @RecordCount  OUTPUT
        
    --if (@RecordCount = 0)    --如果没有,则返回零
                   --return 0
        /*判断页数是否正确*/
          
    if (@intPageNo - 1* @intPageSize > @RecordCount   --页号大于总页数,返回错误
            return (-1)
        
    --set nocount off--打开计数

        
    if @SelectWhere != '' 
            
    begin
                
    set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' , '''+str(@RecordCount)+''' as Count from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' where 1=1 and '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
            
    end
        
    else
            
    begin
                
    set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' , '''+str(@RecordCount)+''' as Count from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+''+@SelectOrder
            
    end
        
    execute sp_executesql @TmpSelect
        
    return(@@rowcount)
    end
  • 相关阅读:
    HDU5597/BestCoder Round #66 (div.2) GTW likes function 打表欧拉函数
    HDU5596/BestCoder Round #66 (div.2) 二分BIT/贪心
    HDU 5596/BestCoder Round #66 (div.2) GTW likes math 签到
    BZOJ 1877: [SDOI2009]晨跑 费用流
    BZOJ 1452: [JSOI2009]Count 二维树状数组
    BZOJ 1143 1143: [CTSC2008]祭祀river 最长反链
    Codeforces Round #335 (Div. 2) D. Lazy Student 贪心
    Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS
    Codeforces Round #335 (Div. 2) A. Magic Spheres 模拟
    UVALive 6187 Never Wait for Weights 带权并查集
  • 原文地址:https://www.cnblogs.com/qixuejia/p/1914823.html
Copyright © 2011-2022 走看看