我可以轻松更新查询结果吗?
假设我有一个大查询返回工资列,我需要根据此查询结果更新工资。
ID - 是我的表的主键
现在,我这样做:
第1步
select id from mytable ...... where something
第2步
update mytable set salary=1000 where id in (select id from mytable ...... where something)
是否存在替代方案?
答案 0 :(得分:0)
是的,您可以轻松直接更新结果。
以下为示例:
update
(
select salary from mytable ...... where something
) set salary=1000
答案 1 :(得分:0)
尝试for update
和current of
。你说你正在寻找像“在网格上更新数据”这样的东西
create table my_table( id number, a varchar2(10), b varchar2(10));
insert into my_table select level, 'a', 'b' from dual connect by level <=10;
select * from my_table;
declare
rec my_table%rowtype;
cursor c_cursor is select * from my_table for update;
begin
open c_cursor;
loop
fetch c_cursor into rec;
exit when c_cursor%notfound;
if rec.id in (1,3,5) then
rec.a := rec.a||'x';
rec.b := rec.b||'+';
update my_table set row = rec where current of c_cursor;
else
delete from my_table where current of c_cursor;
end if;
end loop;
commit;
end;
select * from my_table;