sql 存储过程分页

时间:2022-03-05 09:00:36
首先思想:

利用:
1.
SELECT TOP 页大小 *
FROM TestTable
WHERE (ID >
(SELECT MAX(id)
FROM (SELECT TOP 页大小*页数 id
FROM 表
ORDER BY id) AS T))
ORDER BY ID

需要注意的是如果存在相同记录可能会出错,所以可以将表加入自增队列进行判断即可

2.
:(利用Not In和SELECT TOP 分页 )
语句形式:

SELECT TOP pagesize *
FROM TestTable
WHERE (ID NOT IN
(SELECT TOP pagesize*page id
FROM 表
ORDER BY id))
ORDER BY ID

很容易理解,然后我们转化为存储过程:
CREATE  PROCEDURE hhhb_PageView1
         @PageIndex INT,                             --页面索引,从datagrid中获取
         @PageSize  INT,                              --页面显示数量,从datagrid中获取
         @RecordCount INT OUT,                --返回记录总数
         @PageCount INT OUT,                   --返回分页后页数
         @strGetFields nvarchar(1000),         -- 需要查询的列 首尾要有空格
         @tableName nvarchar(500) ,           --表名称 首尾要有空格
         @ID nvarchar(100),                          --主键,(为表的主键)
         @strWhere  nvarchar(1000) ='',        -- 查询条件 (注意: 不要加 where)
         @sortName nvarchar(50) =' asc ' ,     --排序方式
         @orderName nvarchar(100)              --父级查询排序方式


AS
declare @countSelect nvarchar(2000) 
--设置统计查询语句
if len(@strWhere) =0
--如果没有查询条件
    begin
        set @countSelect=N'SELECT @CountRecord = COUNT(*)  FROM '+@tableName
    end
else
--否则
    begin
        set @countSelect=N'SELECT @CountRecord = COUNT(*)  FROM '+@tableName+' where '+@strWhere
    end
--执行并返回总数
exec sp_executesql @countSelect,N'@CountRecord int output',@RecordCount output
SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)

SET NOCOUNT ON

DECLARE @SQLSTR NVARCHAR(3000)
--实际总共的页码小于当前页码 或者 最大页码
if @PageCount>=0
    --如果分页后页数大于0
    begin
        if @PageCount<=@PageIndex and  @PageCount>0   --如果实际总共的页数小于datagrid索引的页数
            --or @PageCount=1
            begin
                --设置为最后一页
    set @PageIndex=@PageCount-1
            end
        else if @PageCount<=@PageIndex and  @PageCount=0
            begin
                set @PageIndex=0;
            end
    end

IF @PageIndex = 0 OR @PageCount <= 1  --如果为第一页
    begin
        if len(@strWhere) =0
            begin
                SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+@strGetFields+'  FROM  '+@tableName+' ORDER BY '+@orderName+@sortName
            end
        else
            begin
                SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+@strGetFields+'  FROM  '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName
            end
    end
ELSE IF     @PageIndex = @PageCount - 1 --如果为最后一页           
    begin
        if len(@strWhere) =0
            begin
                SET @SQLSTR =N' SELECT '+@strGetFields+'  FROM '+@tableName+' where '+@ID+' not in  ( SELECT TOP '+STR(/*@RecordCount - */@PageSize * @PageIndex )+@ID+'  FROM  '+@tableName+'ORDER BY '+@orderName+@sortName+' )   ORDER BY '+@orderName+@sortName
            end
        else
            begin
                SET @SQLSTR =N' SELECT '+@strGetFields+'  FROM '+@tableName+' where '+@ID+' not in  ( SELECT TOP '+STR(/*@RecordCount - */ @PageSize * @PageIndex )+@ID+'  FROM  '+@tableName+' where '+@strWhere+'ORDER BY '+@orderName+@sortName+' )  and '+@strWhere+' ORDER BY '+@orderName+@sortName
            end
    end
ELSE                                                              --否则执行 
    begin
         if len(@strWhere) =0
            begin
                SET @SQLSTR =N' SELECT TOP  '+STR( @PageSize )+@strGetFields+' FROM '+@tableName+' where '+@ID+' not in  ( SELECT TOP '+STR( /*@RecordCount - */@PageSize * @PageIndex )+@ID+'  FROM  '+@tableName+' ORDER BY '+@orderName+@sortName+' )  ORDER BY '+@orderName+@sortName
            end
         else
            begin
                SET @SQLSTR =N' SELECT TOP  '+STR( @PageSize )+@strGetFields+' FROM '+@tableName+' where '+@ID+' not in  (SELECT TOP '+STR(/*@RecordCount - */ @PageSize * @PageIndex )+@ID+'  FROM  '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName+' )and '+@strWhere+'ORDER BY '+@orderName+@sortName
            end
    end

EXEC (@SQLSTR)
/*print (@SQLSTR)*/
set nocount off
GO

调用存储过程:
SqlCommand mySqlCommand = new SqlCommand("sp_PageView", acct.dbt.GetSqlConnection());
mySqlCommand.CommandType = CommandType.StoredProcedure;
SqlParameter workParm;


//搜索表字段,以","号分隔
workParm = mySqlCommand.Parameters.Add("@strGetFields", SqlDbType.NVarChar, 1000);


mySqlCommand.Parameters["@strGetFields"].Value = " a.ProId,b.Supplier_Name_Simple,b.Pro_Config, a.SPXZ, a.CDSX, a.GYS,a.XLBM, a.XLMC, a.YWMC," +
"a.GGSX, a.CMFW, a.HDGG, a.BZGG, a.JHJG, a.JBSJ," +
"a.JBZDSJ, a.ZDSJ,a.ZDZDSJ," +
"c.P_MC AS ZLMC, d .P_MC AS JBMC, e.P_MC AS SCGYMC, f.GGMC AS GGMC," +
"JHJG_Fg_Name = (CASE a.JHJG_Fg WHEN 1 THEN '包含' WHEN 2 THEN '不含' END), " +
"CDSX_Name = (CASE a.CDSX WHEN 1 THEN '国产' WHEN 2 THEN '进口' END), " +
"GGSX_Name = (CASE a.GGSX WHEN 1 THEN '张' WHEN 2 THEN '片' END), " +
"Config_Name = (CASE b.Pro_Config WHEN 1 THEN '四进' WHEN 0 THEN '实尺' END)" ;

//搜索表名
workParm = mySqlCommand.Parameters.Add("@tableName", SqlDbType.NVarChar, 500);
mySqlCommand.Parameters["@tableName"].Value = " Products a INNER JOIN " +
"Suppliers b ON a.GYS = b.Supplier_No INNER JOIN " +
"JBSZ c ON a.ZL = c.ID INNER JOIN &q