日期:2014-05-18  浏览次数:20606 次

数据库更新问题!急!!!!!在线等!
我想要更新一个表中的top   2000条数据   SQL语句应该怎么写?  

例如表名为:ac_id

结构为
ID             NAME           pwd       zt    
1               dsf             4545     0
2               dfsd           333       1
3               fgfg           5632     0
。。。。

我要更新这个表里的 top   2000   条数据 的ZT 改为1 条件是zt=0 order   by   name  

怎么写? 再线等!! 


------解决方案--------------------
--创建测试环境
create table ac_id(ID int,NAME varchar(10),pwd int,zt int)

--插入测试数据
insert ac_id(ID,NAME,pwd,zt)
select '1 ', 'dsf ', '4545 ', '0 ' union all
select '2 ', 'dfsd ', '333 ', '1 ' union all
select '3 ', 'fgfg ', '5632 ', '0 '

--求解过程
update _t
set _t.zt = 1
from ac_id _t
where id in(select top 2000 id from ac_id order by name)

select * from ac_id

--删除测试环境
drop table ac_id

/*--测试结果
ID NAME pwd zt
----------- ---------- ----------- -----------
1 dsf 4545 1
2 dfsd 333 1
3 fgfg 5632 1

(所影响的行数为 3 行)
*/

------解决方案--------------------
另外,order by name 的top 2000 条数据是指
1 top 2000
2 zt等于0的top 2000?
楼上的代码指的是1。
------解决方案--------------------
--如果是2的话
--创建测试环境
create table ac_id(ID int,NAME varchar(10),pwd int,zt int)

--插入测试数据
insert ac_id(ID,NAME,pwd,zt)
select '1 ', 'dsf ', '4545 ', '0 ' union all
select '2 ', 'dfsd ', '333 ', '1 ' union all
select '3 ', 'fgfg ', '5632 ', '0 '

--求解过程
update _t
set _t.zt = 1
from ac_id _t
where id in(select top 2000 id from ac_id where zt = 0 order by name)

select * from ac_id

--删除测试环境
drop table ac_id

/*--测试结果
ID NAME pwd zt
----------- ---------- ----------- -----------
1 dsf 4545 1
2 dfsd 333 1
3 fgfg 5632 1

(所影响的行数为 3 行)
*/

------解决方案--------------------
update table a set a.zt = 1 from ac_id a
where a.id in(select top 2000 id from ac_id order by name)
------解决方案--------------------
update ac_id
set zt = 1
from ac_id
where id in(select top 2000 id from ac_id order by name)