---
title: "万能SQL分页语句"
author: "wjxa"
date: "2010-03-08T23:29:36Z"
tags: ["SQL"]
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2010-03-09T01:10:55Z"
id: 610819
url: "https://wjxa.iteye.com/blog/610819"
---

```SQL

1.select top 每页显示的记录数 * from topic where id not in  
2. (select top （当前的页数-1）×每页显示的记录数 id from topic order by id desc)   
3. order by id desc  

```


存储过程分页


```SQL

create procedure tb_operator_proc    
@pageSize int,      --每页的记录数    
@index int          --从第几条记录开始    
as   
  begin   
  set nocount on        
declare @sql nvarchar(3000)    
if @index>@pageSize    
     begin   
  set @index = @index - @pageSize    
       set @sql='select top '+cast (@pageSize as varchar(8))+' * from tb_operator where id>(    
          select max(id) from (select top '+cast (@index as varchar(8))+' id from tb_operator order by id)     
        as t)    
        order by id'    
  end   
else   
  begin   
      set @sql = 'select top '+cast (@pageSize as varchar(8))+'* from tb_operator order by id'    
  end   
execute sp_executesql @sql   --调用系统存储过程忧化效率     
set nocount off   
end 

```