22,210
社区成员
发帖
与我相关
我的任务
分享
if exists(select name from sysobjects where name = N'pageQuery')--加N表示为常量
drop proc pageQuery
go
create proc pageQuery
@areaId varchar(10)='SH',
@returnCol nvarchar(100)='*',--返回的列,默认值为*
@pageSize int,--每页数据数量
@colname nvarchar(20),--依据排序的列,实际上是根据行号排序
@tableName nvarchar(50),--表名
@pageIndex int,--页码
@isDesc int=0,--是否降序
@where nvarchar(200)='',--where条件子句
@sum int output--记录总条数
as
declare @parm nvarchar(100)--定义调用系统存储过程sp_executesql时需要用到的output参数
set @parm=N'@s int output'--给output参数赋值
declare @tempsql nvarchar(200)--定义临时sql语句
set @tempsql=N'select @s=count(*) from '+@tableName +' where manageResult is null and AreaId=@areaId' --给临时sql语句
declare @sumPage int--定义接收记录总条数的变量
exec sp_executesql @tempsql,@parm,@s=@sum output--执行系统存储过程sp_executesql,将记录总条数返回给@sum变量
set @sumPage=ceiling(@sum/@pageSize)--将@sumPage进一
declare @sql nvarchar(1000)--定义sql语句
if(@isDesc<>0)--如果需要降序查询
begin
set @colName=@colName
end
set @sql='select '+@ReturnCol+' from
(select top '+str(@pageSize)+' * from
(select row_number() over (order by '+@colName+') as num,'+@tableName+'.* from '+@tablename+')
as tempTable where num > '+str((@pageIndex-1)*@pageSize)+')
as tmp '
if(@where<>'')--如果where条件不为空
begin
set @sql=@sql+' where '+@where
end
--if(@areaId<>'')
-- begin
-- set @sql=@sql+' where '+@where + 'and AreaId= ' + @areaId
-- end
exec(@sql)--执行sql语句
return @sumPage--返回总页数
go
declare @sumPage int
declare @sum int
exec @sumPage=pageQuery
'Id,TelType,ClientId,AreaId,DeptName,OutStoreDate,TelDate,ClientPerson',--返回的列
30,--每页存放的数据数量
'Id',--照指定列排序
'Tbl_InputInfor',--对指定表操作
1,--第几页
1,--非0代表降序
'',--where条件
--'SH',
@sum output
select @sumPage as '总页数'
select @sum as '总记录'