如何用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