更新查询时如何更新有限数量的记录?

问题描述

我们有更新查询,该查询更新所有具有相同城市名称和国家/地区的学生记录,但我们只希望更新500个具有相同城市名称和国家/地区的学生

这是到目前为止的查询:

update student st 
set st.Fee_Call_Opt_uid = (select t.opt_uid 
                           from (select distinct eco.opt_uid,eco.employee_id,ct.city_name,con.country_name 
                                 from employee_calling_operator eco 
                                 join territory tr 
                                     on tr.territory_id = eco.territory_id
                                 join city ct 
                                     on ct.territory_id = tr.territory_id
                                 JOIN country con 
                                     on con.country_id = ct.country_id) t
                           where st.city = t.city_name 
                           and st.country = t.country_name  
                           AND st.is_active_flg = 'Y'
                           and t.opt_uid = :P242_OPT_UID);

请帮帮我。我们正在使用oracle 11g

解决方法

您可以使用rownum。 这是一个简单的示例:

update test
set col1 = 2
where rownum <=2 
and col1 is null

这是一个演示:

DEMO

所以只需添加:

where rownum <= 500

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...