110,571
社区成员
发帖
与我相关
我的任务
分享
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
create procedure [dbo].[Proc_DataPager]
(
@TableName nvarchar(12), --表名
@PageSize int, --分页大小
@PageCur int, --当前页码
@QueryField nvarchar(256)='*', --要查询的字段
@StrWhere nvarchar(256)='', --查询条件
@SortField nvarchar(12), --排序字段
@AscOrDesc int=0 --排序方式0降序1升序
)
as
begin
declare @BeginIndex int --开始查询索引位置
declare @EndIndex int --结束查询索引位置
declare @SortType nvarchar(12) --排序方式
declare @StrSQL nvarchar(3000) --sql查询语句
set @BeginIndex=@PageSize*@PageCur
set @EndIndex=@BeginIndex+@PageSize
if @AscOrDesc=0
set @SortType=' desc'
else
set @SortType=' asc'
set @StrSQL='select * from (select row_number() over (order by '+@SortField+@SortType+' ) as rn, '+@QueryField+' from '+@TableName
--查询条件不为空
if @StrWhere!=''
begin
set @StrSQL=@StrSQL+' where '+@StrWhere
end
set @StrSQL=@StrSQL+') as _table where rn>'+cast(@BeginIndex as nvarchar)+' and rn<='+cast(@EndIndex as nvarchar)
exec (@StrSQL)
end