首页 > 解决方案 > 如何在更新查询时更新有限数量的记录?

问题描述

我们有更新查询,它会更新所有具有相同城市名称和国家/地区的学生记录,但我们只想更新 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

标签: sqloracleplsqloracle11g

解决方案


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

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

这是一个演示:

演示

所以只需添加:

where rownum <= 500

推荐阅读