Solve “cannot perform a DML operation inside a query ” [duplicate]

邮差的信 提交于 2019-12-24 12:35:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!