SQL万能分页存储过程
浏览量:2614
create proc fenye
(
@pageindex int,--行数--
@pagenum int,--页次--
@strsql nvarchar(4000),--SQL语句--
@order nvarchar(50),--排序字段--
@count int output --输出参数,总行数--
)
as
declare @orderdesc nvarchar(100)
declare @orderasc nvarchar(100)
declare @sql nvarchar(4000)
declare @rows int
begin
set @sql='select @a=count(*) from (' +@strsql+ ') as a '
exec sp_executesql @sql,N'@a int output',@count output
set @rows=@pageindex*@pagenum
if(@rows>@count)
set @pageindex=@pageindex-(@rows-@count)
if(@pageindex<0)
set @pageindex=0
if @order!=''
begin
set @orderdesc='order by '+@order+' desc'
set @orderasc='order by '+@order+' asc'
end
else
begin
set @orderdesc='order by id desc'
set @orderasc='order by id asc'
end
exec('
select * from (
select top '+@pageindex+' * from(
select * from (
select top ('+@rows+') * from ('+@strSql+' ) as a '+@orderdesc+')
as a ) as a '+@orderasc+') as a '+@orderdesc+'
')
end
@pageindex int,--行数--
@pagenum int,--页次--
@strsql nvarchar(4000),--SQL语句--
@order nvarchar(50),--排序字段--
@count int output --输出参数,总行数--
)
as
declare @orderdesc nvarchar(100)
declare @orderasc nvarchar(100)
declare @sql nvarchar(4000)
declare @rows int
begin
set @sql='select @a=count(*) from (' +@strsql+ ') as a '
exec sp_executesql @sql,N'@a int output',@count output
set @rows=@pageindex*@pagenum
if(@rows>@count)
set @pageindex=@pageindex-(@rows-@count)
if(@pageindex<0)
set @pageindex=0
if @order!=''
begin
set @orderdesc='order by '+@order+' desc'
set @orderasc='order by '+@order+' asc'
end
else
begin
set @orderdesc='order by id desc'
set @orderasc='order by id asc'
end
exec('
select * from (
select top '+@pageindex+' * from(
select * from (
select top ('+@rows+') * from ('+@strSql+' ) as a '+@orderdesc+')
as a ) as a '+@orderasc+') as a '+@orderdesc+'
')
end
CRM定制,办公OA,软件开发,沈阳易势科技最专业
下一篇:SQL提取数字字母汉字