sql 带输出参数的存储过程分页

USE [test]
GO
/****** Object:  StoredProcedure [dbo].[Proc_Paging]    Script Date: 2018/10/8/周一 8:26:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Author,,Name>
-- Create date: 2018-09-30 <Create Date,,>
-- Description:    <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Proc_Paging]
@pageNo int =1,    --当前页码
@pageSize int=3,    --每页记录数
@pageCount int output,    --总页数
@key varchar(max)    -- 查找条件
AS
BEGIN
       -- 定义变量
       declare @endRow int        --结束行的编号
       declare @startRow int    --起始行的编号
       declare @recordCount int    --所有记录的数量
  
       -- 计算
       set @startRow=((@pageNo-1)*@pageSize)+1
       set @endRow=(@pageNo*@pageSize)
       -- 查询数据
       --select * from 
       --(
       --   select row_number() over(order by id) as RowID,* from student
       --) tempTable
       --where RowID between @startRow and @endRow

       declare @cmdText varchar(max)
       set @cmdText='select * from
                     ( 
                        select row_number() over(order by id) as RowID,* from student '+@key+'
                     )tempTable 
                     where RowID between '+convert(varchar,@startRow)+' and '+convert(varchar,@endRow)+' ';
       exec(@cmdText)
       -- 计算总页数
       /*
            --@sql 注意类型必须是nvarchar
            --N不可少
       */
       declare @cmdText2 nvarchar(max),@count int
       set @cmdText2='select @tempCount=count(*) from student '+@key+' '
       exec sp_executesql @cmdText2,N'@tempCount int output',@recordCount output
       -- 向上取整数
       set @pageCount= ceiling(@recordCount*1.0/@pageSize)
END
--declare @recordCount2 int
--exec [Proc_Paging] 1,10,@recordCount2 output,'where id=20'
--select @recordCount2

猜你喜欢

转载自blog.csdn.net/MyNameIsXiaoLai/article/details/82964791