oracle slides03 fp2005 ver 1.0

98 415 0
oracle  slides03  fp2005  ver 1.0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

PL/SQL Oracle Day 3 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Objectives To explain PL/SQL subprograms units • Procedures • Functions • Packages • Database Triggers 3 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Overview of Subprograms A subprogram: • Is a named PL/SQL block that can accept parameters and be invoked from a calling environment • Is of two types: – A procedure that performs an action – A function that computes a value • Is based on standard PL/SQL block structure • Provides modularity, reusability, extensibility, and maintainability • Provides easy maintenance, improved data security and integrity, improved performance, and improved code clarity 4 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Block Structure for Anonymous PL/SQL Blocks DECLARE (optional) Declare PL/SQL objects to be used within this block BEGIN (mandatory) Define the executable statements EXCEPTION (optional) Define the actions that take place if an error or exception arises END; (mandatory) 5 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Block Structure for PL/SQL Subprograms <header> IS | AS Declaration section BEGIN Executable section EXCEPTION Exception section END; Subprogram Specification Subprogram Body 6 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 PL/SQL Subprograms xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx Code repeated more than once in a PL/SQL program Subprogram A, which contains the repeated code xxx xxx xxx xxx xxx xxx PL/SQL program invoking the subprogram at multiple locations A A A 7 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Benefits of Subprograms • Easy maintenance • Improved data security and integrity • Improved performance • Improved code clarity 8 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 What Is a Procedure? • A procedure is a type of subprogram that performs an action. • A procedure can be stored in the database, as a schema object, for repeated execution. 9 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Syntax for Creating Procedures CREATE [OR REPLACE] PROCEDURE procedurename (parameter1 [mode] datatype1, parameter2 [mode] datatype2, . . .) IS|AS PL/SQL Block; • The REPLACE option indicates that if the procedure exists, it will be dropped and replaced with the new version created by the statement. • PL/SQL block starts with either BEGIN or the declaration of local variables and ends with either END or END procedurename. 10 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Developing Procedures 1 Editor Code to create procedure filename.sql SQL*Plus 2 Load and execute file.sql Source code Compile P code Use SHOW ERRORS to view compilation errors Procedure created Execute 3 Oracle [...]... RETURN statement Copyright © 2005, Infosys Technologies Ltd 27 ER/CORP/CRS/DB25/003 Version No 2.0 Creating a Function Editor Code to create function 1 iSQL*Plus 2 Load and execute filename.sql Source code Oracle Compile Function created P code Invoke Copyright © 2005, Infosys Technologies Ltd 3 28 ER/CORP/CRS/DB25/003 Version No 2.0 Executing Functions • • • Invoke a function as part of a PL/SQL expression... variable Cannot be assigned a default value 13 ER/CORP/CRS/DB25/003 Version No 2.0 IN Parameters: Example p_id 176 CREATE OR REPLACE PROCEDURE inc_salary (p_id IN empl.emp_id%TYPE) IS BEGIN UPDATE empl SET salary = salary * 1.10 WHERE emp_id = p_id; END inc_salary; / Copyright © 2005, Infosys Technologies Ltd 14 ER/CORP/CRS/DB25/003 Version No 2.0 OUT Parameters: Example Calling environment QUERY_EMP... ER/CORP/CRS/DB25/003 Version No 2.0 OUT Parameters: Example CREATE OR REPLACE PROCEDURE query1 (p_id IN empl.emp_id%TYPE, p_name OUT empl.name%TYPE, p_salary OUT empl.salary%TYPE, p_comm OUT empl.commission%TYPE) IS BEGIN SELECT name, salary, commission INTO p_name, p_salary, p_comm FROM empl WHERE emp_id = p_id; END query1; / Copyright © 2005, Infosys Technologies Ltd 16 ER/CORP/CRS/DB25/003 Version No 2.0... :g_comm) PRINT g_name Copyright © 2005, Infosys Technologies Ltd 17 ER/CORP/CRS/DB25/003 Version No 2.0 Viewing IN OUT Parameters VARIABLE g_phone VARCHAR2(15) BEGIN :g_phone := ‘919885260358'; END; / PRINT g_phone EXECUTE Phone_proc (:g_phone) PRINT g_phone Copyright © 2005, Infosys Technologies Ltd 18 ER/CORP/CRS/DB25/003 Version No 2.0 Methods for Passing Parameters • • • Positional: List actual parameters... Ltd 19 ER/CORP/CRS/DB25/003 Version No 2.0 DEFAULT Option for Parameters CREATE OR REPLACE PROCEDURE add_department (p_name IN dep.dep_name%TYPE DEFAULT ‘abc', p_loc IN dep.loc_id%TYPE DEFAULT 1900) IS BEGIN INSERT INTO dep(dep_id, dep_name, loc_id) VALUES (seq1.NEXTVAL, p_name, p_loc); END add_department; / Copyright © 2005, Infosys Technologies Ltd 20 ER/CORP/CRS/DB25/003 Version No 2.0 Examples of... 21 ER/CORP/CRS/DB25/003 Version No 2.0 Declaring Subprograms CREATE OR REPLACE PROCEDURE empl_leave (p_id IN empl.emp_id%TYPE) IS PROCEDURE log_exec1 IS BEGIN INSERT INTO log_table1 (user_id, log_date) VALUES (USER, SYSDATE); END log_exec1; BEGIN DELETE FROM empl WHERE emp_id = p_id; log_exec1; END emp_leave; / Copyright © 2005, Infosys Technologies Ltd 22 ER/CORP/CRS/DB25/003 Version No 2.0 Invoking... 2005, Infosys Technologies Ltd 23 ER/CORP/CRS/DB25/003 Version No 2.0 Invoking a Procedure from Another Procedure CREATE OR REPLACE PROCEDURE emp_process IS CURSOR c1 IS SELECT emp_id FROM empl; BEGIN FOR i IN c1 LOOP inc_salary(emp_rec.employee_id); END LOOP; COMMIT; END emp_process; / Copyright © 2005, Infosys Technologies Ltd 24 ER/CORP/CRS/DB25/003 Version No 2.0 Removing Procedures Drop a procedure... 2005, Infosys Technologies Ltd 25 ER/CORP/CRS/DB25/003 Version No 2.0 Stored Functions • • • A function is a named PL/SQL block that returns a value A function can be stored in the database as a schema object for repeated execution A function is called as part of an expression Copyright © 2005, Infosys Technologies Ltd 26 ER/CORP/CRS/DB25/003 Version No 2.0 Syntax for Creating Functions CREATE [OR...Formal Versus Actual Parameters •Formal parameters: variables declared in the parameter list of a subprogram specification Example: CREATE PROCEDURE in_salary(p_id NUMBER, p_amount NUMBER) END inc_salary; •Actual parameters: variables or expressions referenced in the parameter list of a subprogram call Example: in_salary(v_id, 2000) Copyright © 2005, Infosys Technologies Ltd 11 ER/CORP/CRS/DB25/003 Version... ER/CORP/CRS/DB25/003 Version No 2.0 Advantages of User-Defined Functions in SQL Expressions • • • Extend SQL where activities are too complex, too awkward, or unavailable with SQL Can increase efficiency when used in the WHERE clause to filter data, as opposed to filtering the data in the application Can manipulate character strings Copyright © 2005, Infosys Technologies Ltd 30 ER/CORP/CRS/DB25/003 Version No . procedure p_id p_name p_salary p_incentives 18 9 Ram 8 800 0. 30 16 Copyright © 200 5, Infosys Technologies Ltd ER/CORP/CRS/DB25 /00 3 Version No. 2 .0 OUT Parameters: Example CREATE OR REPLACE PROCEDURE query1 (p_id IN empl.emp_id%TYPE, . list of a subprogram call Example: in_salary(v_id, 200 0) 12 Copyright © 200 5, Infosys Technologies Ltd ER/CORP/CRS/DB25 /00 3 Version No. 2 .0 Procedural Parameter Modes Calling environment Procedure (DECLARE) BEGIN EXCEPTION END; IN. UPDATE empl SET salary = salary * 1. 10 WHERE emp_id = p_id; END inc_salary; / 15 Copyright © 200 5, Infosys Technologies Ltd ER/CORP/CRS/DB25 /00 3 Version No. 2 .0 OUT Parameters: Example Calling

Ngày đăng: 18/04/2014, 10:25

Từ khóa liên quan

Mục lục

  • PL/SQL Oracle Day 3

  • Objectives

  • Overview of Subprograms

  • Block Structure for Anonymous PL/SQL Blocks

  • Block Structure for PL/SQL Subprograms

  • PL/SQL Subprograms

  • Benefits of Subprograms

  • What Is a Procedure?

  • Syntax for Creating Procedures

  • Developing Procedures

  • Formal Versus Actual Parameters

  • Procedural Parameter Modes

  • Creating Procedures with Parameters

  • IN Parameters: Example

  • OUT Parameters: Example

  • Slide 16

  • Viewing OUT Parameters

  • Viewing IN OUT Parameters

  • Methods for Passing Parameters

  • DEFAULT Option for Parameters

Tài liệu cùng người dùng

Tài liệu liên quan