一般用分页语句   select top 5  *  from (表名) where id  not in( select top 0  id  from (表名))这个对单独查一张表还可以,那如果是查询的数据是通过2张表的ID查出来的 应该如何写,比如: select * from table1 a, table2 b where a.id=b.id; 有没有通过拼接SQL 分页的sql语句(sqlserver的)

解决方案 »

  1.   

    select top pagesize*page * from (
    select   *   from   table1   a,   table2   b   where   a.id=b.id;   
    ) t1  
    where   t1.id     not   in(   select   top   (page-1)*pagesize     id     from   (
    select   *   from   table1   a,   table2   b   where   a.id=b.id;   
    ) t2 )你看这样可以吗?
      

  2.   

    修改一下了select  top   pagesize*page   *   from 

          select * from table1 a, table2 b where a.id=b.id; 
    )   
    t1  where t1.id not in(select top (page-1)*pagesize  t2.id  from
       ( 
          select * from table1 a,table2 b where a.id=b.id; )  t2  
      

  3.   

    我加入这个SQL语句不对
     Select dlid,cvouchtype,bfirst,breturnflag,cbustype,cstname,cdlcode,ddate,cexch_name,iexchrate,ccusabbname,cdepname,cpersonname,cmaker,cverifier,cwhname,cinvcode,cinvaddcode,cinvname,cinvstd,iquantity,iquotedprice,itaxunitprice,iunitprice,imoney,itax,isum,bodytaxrate,kl,kl2,idiscount from TempDB..TMPUF_528361_Lst1147 Where IdOrder  >= 1 And IDOrder <= 500 order by IdOrder
      

  4.   

    报 除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。
      

  5.   

    先谢了  把order by 去掉搞定