sqlserver 数据库存储过程分页

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


ALTER   PROCEDURE CMS_SearchGetDataByPage  
    @tblName      varchar(255),       -- 表名  
    @fldNames      varchar(1000),     -- 选择的字段列表以,分隔  
    @selectOrderFldName      varchar(500),  -- 排序字段以,分隔(不能含keyFldName指定的字段,可为空)  
    @orderFldDesc      varchar(500),  -- 排序字段及排序方向,如addDate desc,id desc(排序字段需通过selectOrderFldName指定,可为空)  
    @keyFldName      varchar(255),     -- 主键字段  
    @PageSize     int = 10,           -- 页尺寸  
    @PageIndex    int = 1,            -- 页码  
    @strWhere     varchar(1000) = ''  -- 查询条件(注意: 不要加where)  
AS  
declare @strWhereA varchar(1200)            -- 临时变量,给sqlwhere加where  
declare @strOrderA varchar(2000)            -- 第一次排序类型  
declare @strOrderB varchar(2000)            -- 第二次排序类型  
declare @strSqlA varchar(4000)          -- 第一次选出  
declare @strSqlB varchar(8000)          -- 第二次选出  
declare @strSQL varchar(8000)           -- 最后选出  
/* 条件*/  
if @strWhere != ''  
    set @strWhereA = ' where ' + @strWhere  
else  
    set @strWhereA = ''  
      
/* 选择字段列表*/  
if @fldNames is null or rtrim(@fldNames) = ''  
    set @fldNames = '*'  
      
/* 排序字段列表*/  
if not(@selectOrderFldName is null or rtrim(@selectOrderFldName) = '')  
    if rtrim(@selectOrderFldName) = 'id'  
        set @selectOrderFldName = ''  
    else  
        set @selectOrderFldName = ',' + @selectOrderFldName  
      
/* 构建order,按指定方式排序*/  
if @orderFldDesc is null or rtrim(@orderFldDesc) = ''  
    set @orderFldDesc = ' order by id desc'  
else  
    set @orderFldDesc = ' order by ' + @orderFldDesc  
set @strOrderA = UPPER(@orderFldDesc)  
set @strOrderB =  replace(@strOrderA,'DESC','DESC1')  
set @strOrderB =  replace(@strOrderB,'ASC','DESC')  
set @strOrderB =  replace(@strOrderB,'DESC1','ASC')  

/* 第一页*/  
if @PageIndex = 1  
    set @strSQL = 'select top ' + str(@PageSize) + ' ' + @fldNames + ' from [' + @tblName + '] with(nolock)' + @strWhereA + ' ' + @strOrderA  
else  
    begin  
        --取得总记录数  
        declare @sql nvarchar(500)  
        declare @maxCount int  
        declare @maxPage int  
        declare @tempRowCount int  
        set @sql ='select @maxCount = count('+@keyFldName+') from [' + @tblName + ']' + @strWhereA  
        exec sp_executesql @sql,N'@maxCount int output',@maxCount output  
        set @maxPage = @maxCount / @PageSize  
        if(@maxCount % @PageSize > 0)  
            set @maxPage = @maxPage + 1  
        /* 最后一页*/  
        if @PageIndex >= @maxPage  
            begin  
                set @PageIndex = @maxPage  
                set @strSqlA = char(13) + '(select top '+str(@maxCount % @PageSize)+' ' + @keyFldName + @selectOrderFldName + ' from [' + @tblName + '] as a with(nolock) ' + @strWhereA + @strOrderB + '  )' + char(13)  
                set @strSqlB = char(13) + '(select ' + @keyFldName +' from ' + @strSqlA  + ' as b )' + char(13)  
                set @strSQL = 'select ' + @fldNames + ' from [' + @tblName + '] where ([' + @keyFldName + '] in '+@strSqlB+')' + @strOrderA + char(13)  
            end  
        else  
            begin  
                /* 不是第一页,也不是最后一页*/  
                if(@PageIndex <= @maxPage / 2)  
                    begin  
                    --前半数的页  
                        set @tempRowCount = @PageIndex * @PageSize  
                        /* 构建SQL,本分页算法的目的是为了实现高效的非主键排序的分页。by tony */  
                        /* 1、先按指定字段+主键字段按降序选出perPage*pageNum条记录*/  
                        set @strSqlA = char(13) + '(select top '+str(@tempRowCount)+' ' + @keyFldName + @selectOrderFldName + ' from [' + @tblName + '] as a  with(nolock) ' + @strWhereA + @strOrderA + '  )' + char(13)  
                        /* 2、再从选出的记录中按升序选出perPage条记录*/  
                        set @strSqlB = char(13) + '(select top '+str(@PageSize)+' ' + @keyFldName + ' from ' + @strSqlA  + ' as b ' + @strOrderB + '  )' + char(13)  
                        /* 3、从数据库中选出主键在第二次选出的记录中的记录,按降序排列,分页完成*/  
                        set @strSQL = 'select ' + @fldNames + ' from [' + @tblName + '] where ([' + @keyFldName + '] in '+@strSqlB+')' + @strOrderA  
                    end  
                else  
                    begin  
                    --后半数的页  
                        set @tempRowCount = @maxCount - (@PageIndex -1) * @PageSize  
                        /* 构建SQL,本分页算法的目的是为了实现高效的非主键排序的分页。by tony */  
                        /* 1、先按指定字段+主键字段按降序选出perPage*pageNum条记录*/  
                        set @strSqlA = char(13) + '(select top '+str(@tempRowCount)+' ' + @keyFldName + @selectOrderFldName + ' from [' + @tblName + '] as a  with(nolock) ' + @strWhereA + @strOrderB + '  )' + char(13)  
                        /* 2、再从选出的记录中按升序选出perPage条记录*/  
                        set @strSqlB = char(13) + '(select top '+str(@PageSize)+' ' + @keyFldName + ' from ' + @strSqlA  + ' as b ' + @strOrderA + '  )' + char(13)  
                        /* 3、从数据库中选出主键在第二次选出的记录中的记录,按降序排列,分页完成*/  
                        set @strSQL = 'select ' + @fldNames + ' from [' + @tblName + '] where ([' + @keyFldName + '] in '+@strSqlB+')' + @strOrderA  
                    end  
                end  
    end  

set nocount on  

/*print @strSQL*/ --显示SQL  
exec (@strSQL)  
set nocount off  
RETURN  

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

 

 

 

		String tblName = "t_product";
		String fldNames = "*";
		String selectOrderFldName = page.getOrderBy();
		String orderFldDesc = page.getOrderBy()+ " "+ page.getOrder() ;
		String keyFldName = "p_id";
		int PageSize = pageSize;
		int PageIndex = pageNo;
		String strWhere = conditionSQL;
		
		String exeString = "CMS_SearchGetDataByPage '"+tblName+"', '"+fldNames+"', '"+selectOrderFldName+"', '"+orderFldDesc+"', '"+keyFldName+"', "+PageSize+", "+PageIndex+", '"+strWhere+"'";

 

你可能感兴趣的:(sql,cms,算法,Go)