不难,不过效率是不可能高的以pubs 下的sales表为例,查找第5条到结束的select a.* from sales a left join (select top 4 * from sales) b on
a.stor_id=b.stor_id and 
a.ord_num=b.ord_num and 
a.ord_date=b.ord_date and 
a.qty=b.qty and 
a.payterms=b.payterms and 
a.title_id=b.title_id
where b.stor_id is null and
b.ord_num is null and
b.ord_date is null and
b.qty is null and
b.payterms is null and
b.title_id is null
需要把所有列都要列举出来!
自编测试数据:(可适用于有重复记录的)
create table #1(id int,num int)
insert #1 select 1,1
insert #1 select 1,1
insert #1 select 2,2
insert #1 select 3,3
insert #1 select 4,4
insert #1 select 5,5
insert #1 select 6,6
查第5到结束:select a.* from #1 a left join (select top 4 * from #1) b on
a.id=b.id and
a.num=b.num
where b.id is null and
b.num is null结果:
id          num         
----------- ----------- 
4           4
5           5
6           6(3 row(s) affected)

解决方案 »

  1.   

    标准SQL基本语句不支持top,麻烦,则要把TOP改写,根据排序字段
    select top 4 * from #1
    ==》
    select * from #1 a where
    (select count(*) from #1 where id<=a.id and num<=a.num ) <=4
    (要没有重复记录,而且按照排序选)根据排序字段,这样可以吗?
    select * from (
    select *,seq=(select count(*) from #1 where id<=a.id and num<=a.num ) from #1 as a) as a
    where seq>5
    order by id,num
      

  2.   

    把所有字段列出来这个条件在我的要求中难以满足
    因为我并不知道它的所有字段(不过极端一点来说,查系统表是可以得到的,可是这样就得动态sql语句了,这种同样是不行的)
    所以Jianli2004(健力)(乡村工程师) 的方法是不可行的
    不过还是谢谢你
      

  3.   

    begin tran
       set rowcount 2 
       delete from t
       set rowcount 0
       select * into #tmp from t
       rollback
    select * from #tmp
    --这样不知可不可以,我没调试,没有好办法就歪的来
      

  4.   

    try this.
    DECLARE @p1 int
    SET @p1=null
    DECLARE @p2 int
    SET @p2=98305
    DECLARE @p3 int
    SET @p3 =311300
    DECLARE @p4 intDECLARE @cnt int
    EXEC sp_cursoropen @p1 OUT,N'SELECT * FROM tableName,@p2 OUTPUT,@p3 OUTPUT,@p4 OUTPUTSET @cnt=@p4-100EXEC sp_cursorfetch @p1,16,101,@cntEXEC sp_cursorclose @p1
      

  5.   


    alter table b add [ident] [int] IDENTITY (1, 1) NOT NULL 
    select * from b where ident>100
    alter table drop column ident
      

  6.   

    大家注意是一条SQL语句完成
    因为这个语句必须可以被其它SQL语句嵌套
      

  7.   

    N_chow(xml beginner) 做的是不是可以??
    这种方法我没见过~但是我知道用定义变量,做循环~当变量为空回滚就知道他的列数了~