



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The notes on PL/SQL, which are very important and belong to the domain of DBMS, will prove to be very effective for students.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!
A procedure is a logically grouped set of SQL and PL/SQL statements that perform a specific task. They are the named PL/SQL block that can be compiled and stored in one of the Oracle engine’s system tables. Procedures and Functions are made up of:
Procedures and Functions are stored in Oracle database but before storing they are compiled and parsed by Oracle engine. Where do stored procedure and functions reside? Procedure and function is stored in the oracle database. They can be invoked or called by any PL/SQL block. Before a procedure or function is stored, the oracle engine parses and compiles the procedure or function. The following steps are performed automatically by oracle engine while creating procedure.
For displaying error using select query SELECT * FROM USER_ERRORS; When the procedure and function is invoked, the oracle engine loads the compiled procedure and function in memory area is called the SYSTEM GLOBAL AREA(SGA). This allows the code to be executed quickly.
How does the oracle engine execute procedures/functions? Verifies user access Verifies procedure or function validity. Execute the procedure or function. To check status of procedure and function is shown by use of select statement as follows:
Procedures Vs Functions
Hello World PL/SQL procedure successfully completed.
IN & OUT Mode Example 1 This program finds the minimum of two values, here procedure takes two numbers using IN mode and returns their minimum using OUT paramters.
SQL> create or replace PROCEDURE findMin(x IN number, y IN number, z OUT number) IS BEGIN IF x < y THEN z:= x; ELSE z:= y; END IF; END; /
Procedure created.
SQL> DECLARE 2 a number; 3 b number; 4 c number; 5 BEGIN 6 a:= 23; 7 b:= 45; 8 findMin(a, b, c); 9 dbms_output.put_line(' Minimum of (23, 45) : ' || c); 10 END; 11 / Minimum of (23, 45) : 23
PL/SQL procedure successfully completed. Methods for Passing Parameters Actual parameters could be passed in three ways:
In mixed notation, you can mix both notations in procedure call; however, the positional notation should precede the named notation. The following call is legal: findMin(a, b, c, m=>d); But this is not legal: findMin(x=>a, b, c, d); Deleting a Standalone Procedure A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for deleting a procedure is: DROP PROCEDURE procedure-name; So you can drop greetings procedure by using the following statement: BEGIN DROP PROCEDURE greetings; END;/ PL/SQL Functions A PL/SQL function is same as a procedure except that it returns a value. Creating a Function A standalone function is created using the CREATE FUNCTION statement. CREATE [OR REPLACE] FUNCTION function_name [argument IN data type [, ...])] RETURN return_datatype {IS | AS}
Is the definition of function consisting of pl/sql statements.
Example: The following example illustrates creating and calling a standalone function. This function returns the total number of CUSTOMERS in the customers table. We will use the CUSTOMERS table which we had created in PL/SQL Variables chapter: Select * from customers;
Example : CREATE OR REPLACE PROCEDURE raise_salary(emp_id INTEGER,increase REAL) IS curr_salary real; sal_missing EXCEPTION; BEGIN SELECT salary INTO curr_salary from emp WHERE id = emp_id; IF curr_salary IS NULL THEN RAISE sal_missing; ELSE UPDATE emp SET SALary = SALary+INCREASE WHERE id = emp_id; END IF; EXCEPTION WHEN NO_DATA_FOUND THEN INSERT INTO EMP_AUDIT VALUES(emp_id,'no such number'); WHEN SAL_MISSING THEN INSERT INTO EMP_AUDIT VALUES (emp_id,'salary is null'); END RAISE_SALARY; SQL> /
Procedure created.
declare emp number := &emp; incr number := &incr; begin
raise_salary(emp,incr);
end;