Posso atualizar o resultado da consulta facilmente?
Suponha que eu tenha uma consulta grande que retorne a coluna salarial e eu preciso de salários atualizados com base nos resultados dessa consulta.
identidade- é chave primária para minha mesa
Agora eu estou fazendo assim:
PASSO 1
select id from mytable ...... where something
PASSO 2
update mytable set salary=1000 where id in (select id from mytable ...... where something)
Existe alternativa para fazer isso facilmente?
Respostas:
0 para resposta № 1Experimentar for update
e current of
. Você disse que está procurando algo como "atualização de dados na grade"
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;
0 para resposta № 2
Sim, você pode atualizar diretamente o resultado facilmente.
Aqui está um exemplo :
update
(
select salary from mytable ...... where something
) set salary=1000