首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么用update只修改数据库记录的前20条

2012-02-12 
如何用update只修改数据库记录的前20条?最好举一个例子,谢各位了。[解决办法]select *from aupdate a set n

如何用update只修改数据库记录的前20条?
最好举一个例子,谢各位了。

[解决办法]
select *from a

update a set name = 'E '
from a a
join(select top 2 * from a order by id )t on a.id =t.id

select * from a

--result
id name
----------- ----------
1 A
2 B
3 C
4 D

(所影响的行数为 4 行)


(所影响的行数为 2 行)

id name
----------- ----------
1 E
2 E
3 C
4 D

(所影响的行数为 4 行)
[解决办法]
例如:表test(id,empno,empname,departid,salary,indate) --id 為primary key
要求:進入公司日期前20名員工的薪資加200元 
with t1 as
(select top 20 id
from test
order by indate
)
update a
set a.salary= a.salary+200
from test as a
left join t1 as b on a.id = b.id
where b.id is not null

注意:只適用於SQL Server 2005
[解决办法]
SET ROWCOUNT 20
update ....
SET ROWCOUNT 0
[解决办法]
--设置update影响的记录数

SET ROWCOUNT 20
update ....
SET ROWCOUNT 0

热点排行