Updated April 6, 2023
Definition on PL/SQL stored procedure
Stored Procedure in PL/SQL is a named block containing one or more declarative SQL statements in order to perform some specific task. The procedures in PL/ SQL are stored in the database and are invoked through triggers, some other procedures, applications of Java, PHP, etc. Stored Procedure has a header and body. Header containing the name of the procedure along with the parameters to be passed. The body contains the declarative part, the execution part (having the SQL queries), and the exception handling part as well. It basically adds the reusability of the code, just like the functions work in other languages like C, C++, Java, etc.
Below given is the basic syntax of creating a stored procedure in PL/SQL:
CREATE [OR REPLACE] PROCEDURE procedure_name ( <param1 IN/OUT data type [ , . . . ]>)] [IS/ AS] <declaration part> BEGIN ---- procedure body including sql queries ----- EXCEPTION <exception handling part> END procedure_name;
- procedure_name: It specifies the name of the stored procedure to be created.
- [OR REPLACE]: It is an optional value. It basically allows the programmer for the modification in the existing procedure.
- <param1 IN/ OUT data type [, . . .]>: It specifies the list of parameters passed in the procedure in the sequence of parameter name then mode, and lastly, the type of parameter. IN is used to represent the value that is passed from outside the procedure. OUT is used to specify the parameter that returns the value outside the procedure.
- IS: IS keyword is used when the procedure is nested in some other blocks.
- AS: AS keyword is used when the procedure is a standalone procedure.
- BEGIN: It is a keyword that specifies the starting of the code having the main logic and the sql statements that need to be executed. It is a mandatory part.
- EXCEPTION: It is a keyword that contains the exception-handling part, basically the actions that need to be performed in case of an exception. It is an optional part.
- END: It specifies the end of the stored procedure.
How stored procedure works in PL/SQL?
Below given are some of the points describing how the stored procedure work in PL/SQL:
1. Stored Procedures in PL/SQL work similarly to Functions. It has a unique name and is used once they are called, or some specified event triggers.
2. Stored Procedure in PL/ SQL contains the main 2 parts:
1. Header: header part contains the name of the procedure along with the parameters that are to be passed. Parameters include the name, mode,l, and the data type.
2. Body: The body part of being stored procedure is categorized into 3 major categories:
– Declarative part: It is an optional part. As the name suggests, the declaration part includes all the variables, cursors, types, constants, subprograms, etc., to be used in the procedure. All these items are local to the procedure and exit once the execution of the stored procedure is completed.
– Execution part: It is a mandatory part of the stored procedure. It contains the main logic or one or more SQL statements that need to be executed in the stored procedure.
– Exception handling part: It is again an optional part. It contains the code or the actions that need to be performed in order to handle the run-time errors.
3. ‘END’ keyword is used to end the stored procedure.
4. The values can be passed and etched from the procedure in PL/SQL with the help of parameters. These parameters are included in the calling statements of the procedures.
5. In order to execute the standalone Procedure in PL/SQL, the EXECUTE statement is used along with the name of the procedure.
6. Since the Parameters can be passed in different ways in the stored procedures. They are used to pass the values and retrieve the results from them. t is important to understand the ways in which we can pass the parameters in Procedure:
– IN Parameter: This parameter is used to give input in the procedures. These are read-only parameters, but we can reference these parameters inside the procedure, but their value is never changed in the program. By default, oracle considers the mode of the parameter as ‘IN’ if no other mode is specified by the programmer.
– OUT Parameter: This parameter is used to retrieve the output from the Procedures. These parameters can never be referenced in the procedures, but their values can be overwritten, so the values can be changed inside the procedures. The final value of the OUT parameter is returned to the calling program.
– IN OUT Parameter: This parameter can be used for both taking the input and getting the output from the Procedures. They can be referenced inside the procedures, and their values can be overwritten by the procedure itself. This parameter is both readable and writable.
7. One Standalone Stored Procedure can be called from another procedure by simply writing the name of it in the BEGIN block:
BEGIN --- calling procedure by its name--- procedure_name; END; /
8. In order to remove a stored Procedure in PL/SQL, the DROP command is used by the programmer along with the procedure name to be deleted:
DROP PROCEDURE procedure_name;
Below given are some of the examples showing how the stored procedure works in PL/SQL:
CREATE OR REPLACE PROCEDURE display AS BEGIN dbms_output.put_line('Hi!! this is my first code working in stored procedure'); END; /
DECLARE num1 number; num2 number; result number; PROCEDURE calSum(num1 IN number, num2 IN number, result OUT number) IS BEGIN result := num1 + num2; END; BEGIN num1:= 230; num2:= 590; calSum(num1, num2, result); dbms_output.put_line('Result of addition of given numbers : ' || result); END; /
CREATE OR REPLACE PROCEDURE param_passing (stud_name IN VARCHAR2) IS BEGIN dbms_output.put_line ('Hello ' || p_name || ' welcome to the college'); END; / EXECUTE param_passing ('Gourang');
The above description clearly explains what the stored procedure is and how it works in PL/SQL. The stored procedure helps in improving the overall performance of an application and reduces the traffic between the databases. Stored Procedures and Functions in PL/ SQL work only when they are called by the user, or some specified situation arises. They occupy a lot of space in the memory, too, so they must be created when required.
We hope that this EDUCBA information on “PL/SQL stored procedure” was beneficial to you. You can view EDUCBA’s recommended articles for more information.