呵呵  建立一个新表
然后
insert into 新表
select top 10 * from 老表
这样一来新表数据就是前10条了  直接删除老表用新表就可以啦!!1

解决方案 »

  1.   

    那样不行的,删除了原有的表会有其它影响,比如有触发器之类的不允许删除原表的情况。Sorry,我的前提条件没给清楚:不能删除原表!
      

  2.   

    建1临时表
    copy 10 记录到临时表
    清空原表数据,
    从临时表 copy 到原表
    删除临时表
      

  3.   

    问个弱智的问题:怎么从临时表 copy 到原表?
      

  4.   

    我倒。。
    都说这么明白了啊
    我说的那样不行你可以先放在临时表里
    insert into 临时表
    select top 10 * from 老表然后清空 老表数据 这样出发器什么都有吧。
    然后
    insert into 老表
    select * from 临时表
      

  5.   


    if (select count(1) from 表)<=10
       rollback
    else
       delete from 表 where id not exists(select top 10 id from 表)
      

  6.   

    楼上的不行
     SQL Server 啊 努力 奋斗-----------------------------------------------------
      

  7.   


    create  table a(a1 datetime,a2 int,a3 varchar ) 
    insert into a select '4:00',4,1
    insert into a select '4:30',6,1
    insert into a select '5:00',6,1
    insert into a select '5:30',7,1
    insert into a select '6:00',8,1
    insert into a select '6:00',8,2
    insert into a select '6:30',6,1
    insert into a select '7:00',11,1
    insert into a select '7:30',10,1
    insert into a select '8:00',10,2
    insert into a select '8:00',10,1
    insert into a select '8:30',14,1
    insert into a select '8:30',11,2
    insert into a select '9:00',12,1
    insert into a select '9:30',11,1
    insert into a select '10:00',9,1
    insert into a select '10:00',12,2
    insert into a select '10:30',12,1
    insert into a select '10:30',11,2
    insert into a select '11:00',9,1
    insert into a select '11:30',12,1
    insert into a select '12:00',14,2
    insert into a select '12:00',10,1
    insert into a select '13:00',12,2
    insert into a select '15:00',11,1
    insert into a select '15:00',8,2
    insert into a select '16:00',12,1
    insert into a select '16:00',7,2
    insert into a select '17:00',10,1
    insert into a select '17:00',9,2
    insert into a select '18:00',12,2
    insert into a select '18:00',8,1
    insert into a select '19:00',7,1
    insert into a select '20:00',10,1
    insert into a select '21:00',8,1
    insert into a select '22:00',8,1
    insert into a select '23:00',7,1create  table b(a1 datetime,a2 int,a3 varchar ) insert into b SELECT top 10 * FROM a truncate table a insert into a SELECT top 10 * FROM b  SQL Server 啊 努力 奋斗-----------------------------------------------------
      

  8.   


    create  table a(a1 datetime,a2 int,a3 varchar ) 
    insert into a select '4:00',4,1
    insert into a select '4:30',6,1
    insert into a select '5:00',6,1
    insert into a select '5:30',7,1
    insert into a select '6:00',8,1
    insert into a select '6:00',8,2
    insert into a select '6:30',6,1
    insert into a select '7:00',11,1
    insert into a select '7:30',10,1
    insert into a select '8:00',10,2
    insert into a select '8:00',10,1
    insert into a select '8:30',14,1
    insert into a select '8:30',11,2
    insert into a select '9:00',12,1
    insert into a select '9:30',11,1
    insert into a select '10:00',9,1
    insert into a select '10:00',12,2
    insert into a select '10:30',12,1
    insert into a select '10:30',11,2
    insert into a select '11:00',9,1
    insert into a select '11:30',12,1
    insert into a select '12:00',14,2
    insert into a select '12:00',10,1
    insert into a select '13:00',12,2
    insert into a select '15:00',11,1
    insert into a select '15:00',8,2
    insert into a select '16:00',12,1
    insert into a select '16:00',7,2
    insert into a select '17:00',10,1
    insert into a select '17:00',9,2
    insert into a select '18:00',12,2
    insert into a select '18:00',8,1
    insert into a select '19:00',7,1
    insert into a select '20:00',10,1
    insert into a select '21:00',8,1
    insert into a select '22:00',8,1
    insert into a select '23:00',7,1create  table b(a1 datetime,a2 int,a3 varchar ) if ((select count(1) from a)>=10)
    begin
            insert into b SELECT top 10 * FROM a         truncate table a          insert into a SELECT top 10 * FROM b 
         drop table b
    end
    else
      PRINT 'No Data'这个可以了 SQL Server 啊 努力 奋斗-----------------------------------------------------
      

  9.   

    楼上的,sql中有ifif (select count(1) from 表)<=10
       rollback
    else
       delete from 表 where id not exists(select top 10 id from 表)
      

  10.   

    delete from 表 where id not in (select top 10 id from 表)
      

  11.   

    我的又一个要求:不要从原表中先删除全部数据然后再从临时表中把前10条记录copy回去这样的做法,因为这会受其它的外部条件的影响,只希望从表中的最后一条数据一条条删除,删除的同时再进行外部条件影响的处理,这样对数据库的负面影响会减到最小。多谢以上各位的回答!!!
      

  12.   

    begin tran delete from table1 where....if @@rowcount<10 
    begin
     print '记录数小于10,不予删除'
     rollback tran
     return
    endif @@error<>0
    begin
      rollback tran
     return
    end
    commit tran
      

  13.   

    楼上的老兄 transaction 处理得好全面。
    不过 delete from table1 where.... 这句的省略号可能才是我最需要的,这个表没有上面其他人说的 id 列,所以不能用 where id > 10 这类的条件去删除。我想可能只能用 scroll cursor 去做吧 ??? 我对 sql 的 cursor 和其它语言的指针这类的东西实在是头疼啊!
      

  14.   

    Delete Table0 Where PrimaryKey Not In (Select Top 10 PrimaryKey From Table0)