SQL点滴16—SQL分页语句总结

原文:SQL点滴16—SQL分页语句总结

今天对分页语句做一个简单的总结,他们大同小异的,只要理解其中一个其他的就很好理解了。

  

使用top选项

selecttop10*from Orders
a where a.orderid notin(selecttop10 orderid from Orders orderby orderid) orderby a.orderid

  

使用max函数

这种方法的前提是有唯一值的一个列。

selecttop10*from Orders a
 where a.orderid>(selectMAX(orderid) from (selecttop10 orderid from Orders orderby orderid) as orderid)
 orderby orderid

  

使用row_number()

select*from (select ROW_NUMBER() over(orderby orderid) as rownumber,*from Orders) myresult
where rownumber between10and20

selecttop10*from (select ROW_NUMBER() over(orderby orderid) as rownumber,*from Orders) myresult
where rownumber>10

with OrderedResult as(
select*,ROW_NUMBER() over(Orderby orderid) as rownumber from Orders)
select*from OrderedResult where rownumber between10and20

    

使用rowcount设置

begin
declare@first_idvarchar(18),@startrowint
setrowcount10
select@first_id=orderid from Orders orderby orderid
select*from Orders where orderid>@first_idorderby orderid
setrowcount0
end

  

使用临时表

begin
declare@pagelowerboundint
declare@pageupperboundint
set@pagelowerbound=10
set@pageupperbound=20

createtable #pageindex([indexid]intidentity(1,1) notnull,[id]varchar(18))
declare@sqlnvarchar(2000)
set@sql=insert into #pageindex([id]) select top +CONVERT(nvarchar,@pageupperbound)
set@sql=@sql+ orderid from Orders
execute sp_executesql @sql
select a.*from Orders a innerjoin #pageindex b on a.orderid=b.id where b.indexid>@pagelowerboundorderby b.indexid
droptable #pageindex
end

  

使用动态存SQL语句

一个分页存储过程,支持多表连接的情况。原理还是使用主键。

USE [PressErp]
GO
/****** Object:  StoredProcedure [dbo].[UP_GetRecordByPageIndex]    Script Date: 05/06/2013 22:00:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--用途:分页存储过程(对有主键的表效率极高) 
ALTER PROCEDURE [dbo].[UP_GetRecordByPageIndex]
    @tblName      varchar(255),       -- 表名
    @fldName      varchar(255),       -- 主键字段名
    @PageSize     int = 10,           -- 页尺寸
    @PageIndex    int = 1,            -- 页码
    @IsReCount    bit = 0,            -- 返回记录总数, 非 0 值则返回
    @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
    @strWhere     varchar(1000) = ‘‘ -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL   varchar(6000)       -- 主语句
declare @strTmp   varchar(100)        -- 临时变量(查询条件过长时可能会出错,可修改100为1000)
declare @strOrder varchar(400)        -- 排序类型
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
if @IsReCount != 0
    set @strSQL = select count(*) as Total from [ + @tblName + ]+ where  + @strWhere
exec (@strSQL)

 

USE [Press]
GO

/****** Object:  StoredProcedure [dbo].[UP_GetRecordByPage]    Script Date: 09/16/2012 00:26:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



--用途:支持任意排序的分页存储过程 
CREATE PROCEDURE [dbo].[UP_GetRecordByPage]
@tblName1 varchar(255),   -- 主表名 School
@tblName2 varchar(500),        -- 次表以及连接School left join City on School.CityID=City.ID left join County on School.CountyID=County.ID
@fldName varchar(255),   -- 显示字段名 
@OrderfldName varchar(255), -- 排序字段名,只能与一个排序字段名School.ID 
@PageSize int = 10,   -- 页尺寸 
@PageIndex int = 1,   -- 页码 
@IsReCount bit = 1,  -- 返回记录总数, 非 0 值则返回 
@OrderType bit = 0,   -- 设置排序类型, 非 0 值则降序 
@strWhere varchar(1000)=‘‘, -- 查询条件 (注意: 不要加 where) ‘School.SchoolName like ‘‘%浙江%‘‘‘
@IsPrint bit=0       --是否打印
AS

declare @strSQL varchar(6000) -- 主语句 
declare @strTmp varchar(1000)   -- 临时变量(查询条件过长时可能会出错,可修改100为1000)
declare @strOrder varchar(400) -- 排序类型

if @OrderType != 0 
    begin 
    set @strTmp = <(select min 
    set @strOrder =  order by   + @OrderfldName +  desc 
    end 
else 
begin 
    set @strTmp = >(select max 
    set @strOrder =  order by   + @OrderfldName +  asc 
end
set @strSQL = select top  + str(@PageSize) +   + @fldName +  from   
+ @tblName2 +   where   + @OrderfldName +   + @strTmp + (  
+ replace(@OrderfldName,@tblName1,tblTmp) +  ) from (select top  + str((@PageIndex-1)*@PageSize) +    
+ @OrderfldName +   from   + @tblName2 +   + @strOrder + ) as tblTmp) 
+ @strOrder
if @strWhere != ‘‘ 
set @strSQL = select top  + str(@PageSize) +   + @fldName +  from   
+ @tblName2 +   where   + @OrderfldName +   + @strTmp + (  
+ replace(@OrderfldName,@tblName1,tblTmp) +  ) from (select top  + str((@PageIndex-1)*@PageSize) +    
+ @OrderfldName +   from   + @tblName2 +   where  + @strWhere +   
+ @strOrder + ) as tblTmp)  + @strOrder
if @PageIndex = 1 
begin 
set @strTmp = ‘‘ 
    if @strWhere != ‘‘ 
    set @strTmp =  where  + @strWhere
    set @strSQL = select top  + str(@PageSize) +   + @fldName +  from   
    + @tblName2 +   + @strTmp +   + @strOrder 
end
if @IsReCount != 0 
    set @strSQL = @strSQL  +/*----*/+ select count(1) as Total from   + @tblName2 +  
if @IsPrint<>0
    print (@strSQL)
exec (@strSQL)

GO

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。