一句SQL语句解决倒序数据分页提取
此解决方案可能比不上网上那些大段代码的store procedure的性能(性能测试我就免了),我以前也写过那样的Store Procedure或C#代码,不过简单就是美,简单就是高效。以下例子仅供大家参考:
select
top
3
PrintObject,PrintOperator,OperateTime
from
PrintRecord
where
(PrintId
<
ALL
(
select
top
6
PrintId
from
PrintRecord
order
by
PrintId
desc
))
order
by
PrintId
desc
以上3是DataGrid中的PageSize,6是DataGrid中的 PageSize × CurrentPageIndex
发表评论
--我也贴一个,思路基本一样,多用了一个"派生表",性能测试我也免了
--必须有主键
use Northwind
declare @Page int
set @Page = 7
declare @Size int
set @Size = 4
declare @ varchar(8000)
set @ = 'select * from (select Top ' + cast(@Size as varchar(100)) + ' * from (select Top ' + cast((@Page * @Size) as varchar(100)) + ' * from products) T1 ' + 'order by T1.productid desc) T2 order by T2.productid'
select @
exec(@)
--必须有主键
use Northwind
declare @Page int
set @Page = 7
declare @Size int
set @Size = 4
declare @ varchar(8000)
set @ = 'select * from (select Top ' + cast(@Size as varchar(100)) + ' * from (select Top ' + cast((@Page * @Size) as varchar(100)) + ' * from products) T1 ' + 'order by T1.productid desc) T2 order by T2.productid'
select @
exec(@)
这个Blog总有惊喜,收藏
如果不按照主键进行排序怎么办?
select top 3 PrintObject,PrintOperator,OperateTime from PrintRecord where
(PrintId < (select min(PrintId ) from( select top 6 PrintId from PrintRecord order by PrintId desc)) as T) order by PrintId desc
(PrintId < (select min(PrintId ) from( select top 6 PrintId from PrintRecord order by PrintId desc)) as T) order by PrintId desc
功能强大的存储过程,所有权归原作者.
功能强大的存储过程:
create PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名以主键值
@PageSize int = 10, -- 页尺寸
@PageIndex int = 2, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(2000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(5000) -- 临时变量
declare @strOrder varchar(500) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp = ' '
if @strWhere != ''
set @strTmp = ' where (' + @strWhere + ')'
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' '+ @strOrder
end
--print @strSQL
exec (@strSQL)
GO
功能强大的存储过程:
create PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 字段名以主键值
@PageSize int = 10, -- 页尺寸
@PageIndex int = 2, -- 页码
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(2000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(5000) -- 临时变量
declare @strOrder varchar(500) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp = ' '
if @strWhere != ''
set @strTmp = ' where (' + @strWhere + ')'
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' '+ @strOrder
end
--print @strSQL
exec (@strSQL)
GO