• SQL 2005 分页存储过程


    -- =============================================
    -- Description:   <高效分页存储过程,适用于Sql2005以上>
    -- Notes:      <排序字段强烈建议建索引>
    -- Page2005'dyj_vclient','client_id,last_dt','client_id','last_dt','desc','',20,1,0
    -- =============================================
    CREATE Procedure [dbo].[sp_page] 
     @TableName varchar(2000),         --表名
     @Fields varchar(2000) = '*',    --字段名(全部字段为*)
     @PrimaryKey varchar(50),        --主键
     @OrderField varchar(500),      --排序字段(必须!支持多字段)
     @sqlWhere varchar(500) = Null,  --条件语句(不用加where)
     @pageSize int,                --每页多少条记录
     @pageIndex int = 1 ,           --指定当前为第几页
     @TotalPage int output          --返回总页数 
    as
    begin
     
        Begin Tran --开始事务
     
        Declare @sqlnvarchar(4000);
        Declare @totalRecordint;    
        ifisnull(@PrimaryKey,'')=''
          set @PrimaryKey='*'
        --计算总记录数
            
        if (@SqlWhere='' or@sqlWhere=NULL)
           set @sql = 'select @totalRecord = count('+@PrimaryKey +') from ' + @TableName
        else
           set @sql = 'select @totalRecord = count('+@PrimaryKey +') from ' + @TableName + ' where ' + @sqlWhere
     
        EXEC sp_executesql@sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数      
        
        --计算总页数
        select@TotalPage=CEILING((@totalRecord+0.0)/@PageSize)
     
        if (@SqlWhere='' or@sqlWhere=NULL)
           set @sql = 'Select * FROM (select ROW_NUMBER()Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from '+ @TableName 
        else
           set @sql = 'Select * FROM (select ROW_NUMBER()Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from '+ @TableName + ' where ' + @SqlWhere   
           
        
        --处理页数超出范围情况
        if@PageIndex<=0 
           Set @pageIndex = 1
        
        if@pageIndex>@TotalPage
           Set @pageIndex = @TotalPage
     
        --处理开始点和结束点
        Declare @StartRecordint
        Declare @EndRecordint
        
        set @StartRecord =(@pageIndex-1)*@PageSize + 1
        set @EndRecord =@StartRecord + @pageSize - 1
     
        --继续合成sql语句
        set @Sql = @Sql + ') as[page_result_table] where rowId between ' +Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
        
        Exec(@Sql)
       ---------------------------------------------------
        If @@Error<> 0
         Begin
           RollBack Tran
           Return -1
         End
        Else
         Begin
           Commit Tran
           Return @totalRecord ---返回记录总数
          End   
    end

  • 相关阅读:
    Update SSM agent to each EC2 via Bat and bash script
    Shell脚本循环读取文件中的每一行
    通过psexec实现远程安装软件包
    Getting Private/Public IP address of EC2 instance using AWS-cli [closed]
    Python to list users in AWS
    BAT script to set local account password never expire
    Difference between Netbios and Host name
    PowerShell官方文档
    powershell for rename server name
    Create a conditional DNS forwarder on our domain.com to Amazon default DNS provider
  • 原文地址:https://www.cnblogs.com/dcrenl/p/4041139.html
Copyright © 2020-2023  润新知