表结构大致如下id username state 
1   a        0
2   b        0
3   c        0
4   d        0
5   e        0
6   f        0
7   h        0
8   l        0
9   g        0要求 每次查询两行内容(只查询state = 0 的内容),并同时把被查询的内容的 state 修改为 1 

解决方案 »

  1.   

    select top 2 * from tb where state=0update tb set state=1 where id in (select top 2 id from tb where state=0)
      

  2.   


    是不是还要加个pre-condition: id列非空 ?
      

  3.   

    如果id=1的有一条,id=2的有多条的话,就会更新多条。
      

  4.   

    update top(2) tb set state=1
    output inserted.*
    where state=0
    sql 2005的新语法,挺好的
      

  5.   

    select top 2 * from tb where state=0update tb set state=1 where id in (select top 2 id from tb where state=0)比较赞同这种做法,但不晓得是你想要的
      

  6.   

    declare @t table(id int ,username varchar (10), [state]  tinyint)update top (2) #tb
    set state=1
    output inserted.id,inserted.username,inserted.[state] into @t
    where state=0select * from @t
      

  7.   

    create table #tb(id int ,username varchar (10), state  tinyint)
    insert #tb select 1 ,'a', 0 union all
    select 2 ,'b', 0 union all
    select 3 ,'c', 0 union all
    select 4 ,'d', 0 union all
    select 5 ,'e', 0 union all
    select 6 ,'f', 0 union all
    select 7 ,'h', 0 union all
    select 8 ,'l', 0 union all
    select 9 ,'g', 0declare @t table(id int ,username varchar (10), [state]  tinyint)update top (2) #tb
    set state=1
    output inserted.id,inserted.username,inserted.[state] into @t
    where state=0select * from @t
    (9 行受影响)(2 行受影响)
    id          username   state
    ----------- ---------- -----
    1           a          1
    2           b          1(2 行受影响)