在 Oracle 中调用另一个存储过程

2021-12-05 00:00:00 sql oracle stored-procedures

有谁知道从另一个内部调用存储过程的方法,或者即使可能的话?如果是这样,你会怎么做?

Does anyone know of a way, or even if its possible, to call a stored procedure from within another? If so, how would you do it?

这是我的测试代码:

SET SERVEROUTPUT ON;

DROP PROCEDURE test_sp_1;
DROP PROCEDURE test_sp;

CREATE PROCEDURE test_sp
AS
BEGIN
    DBMS_OUTPUT.PUT_LINE('Test works');
END;
/

CREATE PROCEDURE test_sp_1
AS
BEGIN
    DBMS_OUTPUT.PUT_LINE('Testing');
    test_sp;
END;
/

CALL test_sp_1;

推荐答案

您的存储过程按编码工作.问题出在最后一行,它无法调用您的任何一个存储过程.

Your stored procedures work as coded. The problem is with the last line, it is unable to invoke either of your stored procedures.

SQL*Plus 中的三个选项是:callexec 和一个匿名的 PL/SQL 块.

Three choices in SQL*Plus are: call, exec, and an anoymous PL/SQL block.

call 似乎是一个 SQL 关键字,并记录在 SQL 参考中.http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_4008.htm#BABDEHHG 语法图表明需要括号,即使没有参数传递给调用例程.

call appears to be a SQL keyword, and is documented in the SQL Reference. http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_4008.htm#BABDEHHG The syntax diagram indicates that parentesis are required, even when no arguments are passed to the call routine.

CALL test_sp_1();

匿名 PL/SQL 块是不在命名过程、函数、触发器等内部的 PL/SQL.它可用于调用您的过程.

An anonymous PL/SQL block is PL/SQL that is not inside a named procedure, function, trigger, etc. It can be used to call your procedure.

BEGIN
    test_sp_1;
END;
/

Exec 是一个 SQL*Plus 命令,它是上述匿名块的快捷方式.EXEC 将作为 BEGIN 传递给数据库服务器;结束;

Exec is a SQL*Plus command that is a shortcut for the above anonymous block. EXEC <procedure_name> will be passed to the DB server as BEGIN <procedure_name>; END;

完整示例:

SQL> SET SERVEROUTPUT ON
SQL> CREATE OR REPLACE PROCEDURE test_sp 
  2  AS 
  3  BEGIN 
  4      DBMS_OUTPUT.PUT_LINE('Test works'); 
  5  END;
  6  /

Procedure created.

SQL> CREATE OR REPLACE PROCEDURE test_sp_1 
  2  AS
  3  BEGIN
  4      DBMS_OUTPUT.PUT_LINE('Testing'); 
  5      test_sp; 
  6  END;
  7  /

Procedure created.

SQL> CALL test_sp_1();
Testing
Test works

Call completed.

SQL> exec test_sp_1
Testing
Test works

PL/SQL procedure successfully completed.

SQL> begin
  2      test_sp_1;
  3  end;
  4  /
Testing
Test works

PL/SQL procedure successfully completed.

SQL> 

相关文章