SQLServer SQL分页语句

SQLServer SQL分页语句

Here i take jobs table of pubs databse for example,details as follow: 

select top eachpage *  from  jobs where job_id not in ( select top (eachpage*(currentpage-1))  job_id from jobs order by job_id desc)) order by job_id desc;

explain:
1. eachpage : the number of each page show the recorder
2.currentpage: current page

notice:
the red line in the sentence,you should take care of it, because Microsoft SQLServer  TOP Function can't support the expression .it can only support a number.
so before you prepare to buildup the sql sentence ,you should alread calculate the result of the expression about (eachpage*(currentpage-1))  .

Example:
set  eachpage=3
      currentpage=5
 so  (eachpage*(currentpage-1)) =12
select top 3 * from jobs where (job_id not in (select top 12 job_id from jobs order by job_id desc)) order by job_id desc


你可能感兴趣的:(SQLServer SQL分页语句)