Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.

Similar presentations


Presentation on theme: "Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g."— Presentation transcript:

1 Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g

2 2 Lesson A Objectives After completing this lesson, you should be able to: Work with PL/SQL stored units

3 Guide to Oracle 10g3 PL/SQL Stored Program Units A program unit is a self-contained group of program statements that can be used within a large program PL/SQL part written in the form builder that may be called by a form trigger is an example of a unit. The unit you created and executed so far in a form builder or SQL*Plus are anonymous PL/SQL programs which are –Programs submitted to the interpreter and run –Do not interact with other program units –They only exist within forms

4 Guide to Oracle 10g4 PL/SQL Stored Program Units Stored PL/SQL program units are: –program units that other PL/SQL program can reference, –and that other database users can use and execute. What do stored units can do? –It can receive input values from other program units –It can pass output values to other program units Stored unit can be linked to database application components created in Form builder to enhance their functionality.

5 Guide to Oracle 10g5 PL/SQL Stored Program Units It can be either –Server-side program units: stored as database object and execute on the sever (can be used by all users) –Client-side program units: stored in the file system of a client workstation and execute on the client workstation

6 Guide to Oracle 10g6 Creating Stored program Units Stored program units can be either: –Procedures: Can receive multiple input parameters and return multiple output values (or no output values) Can perform an action such insert, update or delete record –Functions: Can receive multiple input parameters and always returns a single output value

7 Guide to Oracle 10g7 Creating Procedures CREATE [OR REPLACE] PROCEDURE procedure_name [ (parameter mod datatype [,parameter]) ] IS [local variable declaration_section] BEGIN executable_section [EXCEPTION exception_section] END [procedure_name]; Header Section body Exception section

8 Guide to Oracle 10g8 Creating Procedures

9 Guide to Oracle 10g9 Creating Procedures The header section defines: –The procedure name (name should be unique ) –The parameters that the procedure receives or delivers (IN, OUT, IN OUT) –IS keyword: follows the parameters list –The local procedure variables Create or replace: instructs the DBMS to create the procedure if it does it exists, otherwise; it replaces the existing one OR REPLACE clause is optional. But if omitted and a procedure exists with the same name, an error occurs

10 Guide to Oracle 10g10 Creating Procedures (parameters list) Parameter name, mod and data type are enclosed in parentheses, each is separated by a comma. Parameter mod: describes how the procedure change the value. It can be: –IN: passed parameter is a Read only value. Cannot be changed by the procedure –OUT: write-only value. Always comes on the left side of an assignment statement –IN OUT: its value can be changed

11 Guide to Oracle 10g11 Creating Procedures (parameters list) The data type cannot specify a length, precision, or scale value for a numerical data type, or maximum length of a character data type. Oracle Database derives the length, precision, or scale of the return value from the environment from which the function is called. When the procedure or function is created and debugged, it ca be called from any application.

12 Guide to Oracle 10g12 Creating Procedures (Example) UPDATE_ENROLLMENT_GRADE: a procedure that updates a student’s grade for a course. It accepts 3 input parameters (student ID, Course section ID and grade) Upon successful, the stored procedure exists as an object in your schema.

13 Guide to Oracle 10g13 Creating Procedures (Example) The (%ROWTYPE) reference data type creates composite variables that reference the entire data record. –Faculty_row FACULTY%ROWTYPE; The variable faculty_row references all of the columns in the FACULTY table, and each column has the same data type as its associated DB column. NO Data Size

14 Guide to Oracle 10g14 Debugging program units (SQL*Plus) Interpreter displays only a warning message. It does not contain a compiler error message nor a line location. Instead, it writes all compiler errors to a system table accessed using USER_ERRORS data dictionary view. It displays a summary listing of compile errors generated by the last program unit that was compiled. Use the SHOW ERRORS command: See Figure 9-12.

15 Guide to Oracle 10g15 Calling a Stored Procedure 1.Execute directly from SQL*Plus command line 2.Create separate PL/SQL program that contains –Command to call stored procedure –Passes parameter values to procedure Calling stored procedure from SQL*Plus command line: EXECUTE procedure_name (parameter1_value, parameter2_value,...); Execute update_enrollment_grade(‘MA100’, 12, ‘B’); –Single quote for character is important. –Variables passed for each parameter must be in same order as parameters appear in parameter declarations list

16 Guide to Oracle 10g16 Calling a Stored Procedure Calling stored procedure from separate PL/SQL program –Similar to calling stored procedure from SQL*Plus command line –Omit EXECUTE command update_enrollment_grade(‘MA100’, 12, ‘B’);

17 Guide to Oracle 10g17 Creating Functions A function is similar to a procedure, except that it returns a single value.

18 Guide to Oracle 10g18 Creating Functions The function name (name should be unique ) The syntax of the parameter list is identical to that of the procedure. After the parameters list, RETURN sentence determines the data type of the return value of the function. (remember that procedure does not return a value, so it contains NO Return statement) After IS, the return_value_variable declares the variable that represents the function return value.

19 Guide to Oracle 10g19 Creating Functions

20 Guide to Oracle 10g20 Calling a Functions Calling a function requires assignment the command to a previously declared variable in the calling program, since the function returns a single data value. variable_name := function_name(parameter1, parameter2,...); Variables passed for parameter values –Must be in same order as parameters appear in function declaration

21 Guide to Oracle 10g21 Calling a Functions Pay attention to to_date function.

22 End of Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g


Download ppt "Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g."

Similar presentations


Ads by Google