--RT,我知道可以delete top (10) * from tb,但是这个top是根据什么排序的呢?
delete top (10) * from tb order by col desc
--加上order by果断报错了,这个2005新加的delete top用法到底神马情况呢

解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-10-12 15:08:29
    -- Version:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
    -- Apr 22 2011 11:57:00 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] int)
    insert [tb]
    select 1 union all
    select 2 union all
    select 3 union all
    select 4 union all
    select 5 union all
    select 6
    --------------开始查询--------------------------
    delete  t  from (select top 5 * from tb order by col desc )t
    select * from tb 
    ----------------结果----------------------------
    /* col
    -----------
    1(1 行受影响)*/
      

  2.   

    ...这样的话delete top的用法不就完全没用上么
      

  3.   

    delete  top (5) *  from tb你自己去试试看可以不??貌似是有错误的
      

  4.   

    delete top (10) * from tb
    只影响到正好线访问到的行数
    delete top (10) * from tb order by col desc
    一般要通过
    cte 
    用row_number产生序号 再根据序号删除 达到order by 的目的
      

  5.   

    TOP ( expression ) [ PERCENT ] 
    指定将要删除的任意行数或任意行的百分比。expression 可以为行数或行的百分比。与 INSERT、UPDATE 或 DELETE 一起使用的 TOP 表达式中被引用行将不按任何顺序排列。在 INSERT、UPDATE 和 DELETE 语句中,需要使用括号分隔 TOP 中的 expression。
      

  6.   


    declare @T table (col int)
    insert into @T
    select 1 union all
    select 2 union all
    select 3 union all
    select 4 union all
    select 5 union all
    select 6 union all
    select 7 union all
    select 8 union all
    select 9 union all
    select 10delete top (5) from @T
    select * from @t
    /*
    col
    -----------
    6
    7
    8
    9
    10
    */
      

  7.   

    嗯,鸡冻了,想说的是delete top (10) from tb然后再想order by
    要么这个top的顺序不是随机的么
      

  8.   

    create table tb(id int)
    insert into tb select 1 union all select 2 union all select 3 union all select 4 union all select 5
    go
    delete top (3) from tb
    go
    select * from tb
    go
    drop table tb
    /*
    id
    -----------
    4
    5(2 行受影响)*/
      

  9.   

    能否:
    delete from table where col in (select col from table top(10) order by col desc )
      

  10.   

    --更正下
    一般
    ;with cte_del
    as
    (select top (n) from tb order by col)
    delete cte_del
      

  11.   

    -- 这样当然可以
    delete from tb where col in (select top(10) col from tb  order by col desc )
      

  12.   


    可以,但应改一下:
    delete table where col in (select  top(10) col from table  order by col desc )
      

  13.   

    delete top(10) from table output deleted.*--看看吧
      

  14.   

    虽然都答非所问,结了吧
    其实我是想问delete top ...后面怎么能加上order by 
    不过貌似微软就不让这样,悲催