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 '
)tempTable
where RowID between '+convert(varchar,@startRow)+' and '+convert(varchar,@endRow)+' ';
exec(@cmdText)
-- 計算總頁數
/*
--N不可少
*/
declare @cmdText2 nvarchar(max),@count int
set @cmdText2='select @tempCount=count(*) from student '[email protected]+' '
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