Home > Database > Mysql Tutorial > How Can I Call One Oracle Stored Procedure from Within Another?

How Can I Call One Oracle Stored Procedure from Within Another?

DDD
Release: 2024-12-24 15:20:11
Original
670 people have browsed it

How Can I Call One Oracle Stored Procedure from Within Another?

Calling Stored Procedures from Within Others in Oracle

In Oracle, one might encounter a scenario where it is necessary to invoke one stored procedure from within another. This can be achieved using various methods, which we will explore in this article.

Test Case

Consider the following test code:

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;
Copy after login

Problem and Solution

The issue with the given code lies in the last line, which attempts to directly invoke the stored procedures. This can be resolved by utilizing one of the following three methods:

1. Call Syntax

Oracle's SQL Reference defines the syntax for calling stored procedures as follows:

CALL <procedure_name>(<argument_list>);
Copy after login

Enclose the procedure name in parentheses, even when no arguments are passed.

CALL test_sp_1();
Copy after login

2. Anonymous PL/SQL Block

In an anonymous PL/SQL block, one can call stored procedures without explicitly naming them.

BEGIN
    test_sp_1;
END;
/
Copy after login

3. Exec Command (SQL*Plus)

SQL*Plus provides the exec command as a shortcut for anonymous blocks. It executes the specified stored procedure as if it were part of an anonymous block.

EXEC test_sp_1;
Copy after login

Example

Here is a fully working example:

SET SERVEROUTPUT ON;
CREATE OR REPLACE PROCEDURE test_sp AS BEGIN DBMS_OUTPUT.PUT_LINE('Test works'); END;
/
CREATE OR REPLACE PROCEDURE test_sp_1 AS BEGIN DBMS_OUTPUT.PUT_LINE('Testing'); test_sp; END;
/
CALL test_sp_1(); -- Direct call
exec test_sp_1; -- SQL*Plus shortcut
BEGIN test_sp_1; END; -- Anonymous PL/SQL block
Copy after login

Output:

Testing
Test works
Testing
Test works
Testing
Test works
Copy after login

The above is the detailed content of How Can I Call One Oracle Stored Procedure from Within Another?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template