05 Procedures



PL/SQL Procedures


What is a Stored Procedure?


A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific task. This is similar to a procedure in other programming languages.

A procedure has a header and a body. The header consists of the name of the procedure and the parameters or variables passed to the procedure. The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block.

A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage.

We can pass parameters to procedures in three ways.
1) IN-parameters
2) OUT-parameters
3) IN OUT-parameters

A procedure may or may not return any value.

General Syntax

General Syntax to create a procedure is:

  1. CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters] 
  2. IS 
  3. Declaration section 
  4. BEGIN 
  5. Execution section 
  6. EXCEPTION 
  7. Exception section 
  8. END; 


IS - marks the beginning of the body of the procedure and is similar to DECLARE in anonymous PL/SQL Blocks. 
The code between IS and BEGIN forms the Declaration section.


The syntax within the brackets [ ] indicate they are optional. 
By using CREATE OR REPLACE together the procedure is created if no other procedure with the same name exists or the existing procedure is replaced with the current code.

The below example creates a procedure shuma which gives the sum of two numbers

  1. CREATE OR REPLACE PROCEDURE shuma
  2. (
  3. n1 IN number,
  4. n2 IN number,
  5. s OUT number
  6. )
  7. IS
  8. --seksioni i deklarimit
  9. BEGIN
  10. s:=n1+n2;
  11. END;


How to execute a Stored Procedure?


There are two ways to execute a procedure.

1) From the SQL prompt.

EXECUTE [or EXEC] procedure_name;



2) Within another procedure – simply use the procedure name.

procedure_name;




Examples


Example 1

Krijoni nje proedure qe gjen shumen e dy numrave.
Procedura merr 2 argumenta hyres dhe nje argument Dales.

Zgjidhje

  1. CREATE OR REPLACE PROCEDURE shuma
  2. (
  3. n1 IN number,
  4. n2 IN number,
  5. s OUT number
  6. )
  7. IS
  8. --seksioni i deklarimit
  9. BEGIN
  10. s:=n1+n2;
  11. END;



Si e perdorim proceduren?



  1. SET SERVEROUTPUT ON
  2. DECLARE
  3. numri1 number;
  4. numri2 number;
  5. sh number;
  6. BEGIN
  7. numri1:=&n1;
  8. numri2:=&n2;
  9. shuma(numri1,numri2,sh);
  10. dbms_output.put_line(' Shuma e '||numri1||' me '||numri2||' eshte : ' ||sh);
  11. END;



Example 2


Te ndertojme nje procedure e cila merr si argment dy numra dhe gjen shumen, diferencen dhe prodhimin e tyre



  1. CREATE OR REPLACE PROCEDURE llogarit
  2. (
  3. n1 IN number,
  4. n2 IN number,
  5. s OUT number,
  6. dif out number,
  7. prod out number
  8. )
  9. IS
  10. --seksioni i deklarimit
  11. BEGIN
  12. s:=n1+n2;
  13. dif:=n1-n2;
  14. prod:=n1*n2;
  15. END;



Ta perdorim proceduren

  1. SET SERVEROUTPUT ON
  2. DECLARE
  3. numri1 number;
  4. numri2 number;
  5. sh number;
  6. dif number;
  7. prod number;
  8. BEGIN
  9. numri1:=&n1;
  10. numri2:=&n2;
  11. llogarit(numri1,numri2,sh,dif,prod);
  12. dbms_output.put_line(' Shuma e '||numri1||' me '||numri2||' eshte : ' ||sh);
  13. dbms_output.put_line(' Diferenca e '||numri1||' me '||numri2||' eshte : ' ||dif);
  14. dbms_output.put_line(' Prodhimi e '||numri1||' me '||numri2||' eshte : ' ||prod);
  15. END;