📄 相互调用.txt
字号:
--##################函数和过程的调用################################
--本试验的目的是:函数和存储过过程在相互调用的限制
create table test10
(
name varchar2(100)
);
/
--创建存储过程
create or replace procedure test12 is
begin
insert into test10(name) values('John');
commit;
end;
/
--函数调用含DML的存储过程
create or replace function test13 return number is
begin
test12;
--调用含DML存储的过程:test12
return 0;
end;
/
--在select 语句中调用含有dml的函数
SQL> select test13 from dual;
select test13 from dual
*
ERROR at line 1:
ORA-14551: cannot perform a DML operation inside a query
ORA-06512: at "SCOTT.TEST12", line 3
ORA-06512: at "SCOTT.TEST13", line 3
ORA-06512: at line 1
--在存储过程中,调用带有dml语句的函数
create or replace procedure test14 is
vid number;
begin
vid:=test13();
dbms_output.put_line(vid);
end;
/
--执行存储过程test14
SQL> set serveroutput on
SQL> exec test14
0
PL/SQL procedure successfully completed.
SQL> select * from test10;
NAME
-----------------------------------------------------------------
John
---在plsql中调用带有dml语句的函数ok
declare
n_r number:=0;
begin
n_r:=test13();
end;
总结:
函数和存储过程可以相互调用,
但是如果函数中调用了带有insert/delete/update的存储过程和这样的语句,
这样的函数将不能在select语句中被调用。oracle建议函数中不使用dml。
而在过程中使用dml.这样,可以使用户定义函数如同oracle内置函数一般使用。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -