问题
The pl SQL script below transfers the amount c of money from account a to b. Why isn't allowed to update the table in the function / how can it be fixed?
create or replace function ueberweisung (a varchar2, b varchar2,c number)
RETURN varchar2 IS
k1 number; -- Variablendeklaration
k2 number;
BEGIN
SELECT saldo into k1
FROM konto
WHERE konto_nr=a;
SELECT saldo into k2
FROM konto
WHERE konto_nr=b;
k1:=k1-c;
k2:=k2+c;
update konto
set saldo = case
when konto_nr=a then k1
when konto_nr=b then k2
end;
commit;
RETURN (c ||'Eur überwiesen von Konto ' || a || 'a uf Konto ' || b);
END ueberweisung;
回答1:
It's part of Restrictions on PL/SQL Functions
in order to guard against nasty side effects and upredictable behavior, the Oracle Server makes it impossible for your stored function in SQL to take any of the following actions: The stored function may not modify database tables.
- It cannot execute an INSERT, DELETE, or UPDATE statement.
Use PL/SQL procedure instead
PL/SQL has two types of subprograms, procedures and functions. Generally, you use a procedure to perform an action and a function to compute a value.
来源:https://stackoverflow.com/questions/56722797/solve-cannot-perform-a-dml-operation-inside-a-query