Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
9

Creating Stored Procedures and Functions

Copyright © 2004, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do
the following:
• Differentiate between anonymous blocks ...
Procedures and Functions
•
•
•

Are named PL/SQL blocks
Are called PL/SQL subprograms
Have block structures similar to ano...
Differences Between Anonymous Blocks
and Subprograms
Anonymous Blocks

Subprograms

Unnamed PL/SQL blocks

Named PL/SQL bl...
Procedure: Syntax

CREATE [OR REPLACE] PROCEDURE procedure_name
[(argument1 [mode1] datatype1,
argument2 [mode2] datatype2...
Procedure: Example
...
CREATE TABLE dept AS SELECT * FROM departments;
CREATE PROCEDURE add_dept IS
dept_id dept.departmen...
Procedure: Example

9-7

Copyright © 2004, Oracle. All rights reserved.
Invoking the Procedure

BEGIN
add_dept;
END;
/
SELECT department_id, department_name FROM
dept WHERE department_id=280;

9...
Function: Syntax

CREATE [OR REPLACE] FUNCTION function_name
[(argument1 [mode1] datatype1,
argument2 [mode2] datatype2,
....
Function: Example
CREATE FUNCTION check_sal RETURN Boolean IS
dept_id employees.department_id%TYPE;
empno
employees.employ...
Invoking the Function

SET SERVEROUTPUT ON
BEGIN
IF (check_sal IS NULL) THEN
DBMS_OUTPUT.PUT_LINE('The function returned
N...
Passing Parameter to the Function
DROP FUNCTION check_sal;
/
CREATE FUNCTION check_sal(empno employees.employee_id%TYPE)
R...
Invoking the Function with a Parameter
BEGIN
DBMS_OUTPUT.PUT_LINE('Checking for employee with id 205');
IF (check_sal(205)...
Summary
In this lesson, you should have learned how to:
• Create a simple procedure
• Invoke the procedure from an anonymo...
Practice 9: Overview
This practice covers the following topics:
• Converting an existing anonymous block to a
procedure
• ...
Practice 9: Overview

9-16

Copyright © 2004, Oracle. All rights reserved.
Upcoming SlideShare
Loading in …5
×

Les09

322 views

Published on

  • Be the first to comment

  • Be the first to like this

Les09

  1. 1. 9 Creating Stored Procedures and Functions Copyright © 2004, Oracle. All rights reserved.
  2. 2. Objectives After completing this lesson, you should be able to do the following: • Differentiate between anonymous blocks and subprograms • Create a simple procedure and invoke it from an anonymous block • Create a simple function • Create a simple function that accepts a parameter • Differentiate between procedures and functions 9-2 Copyright © 2004, Oracle. All rights reserved.
  3. 3. Procedures and Functions • • • Are named PL/SQL blocks Are called PL/SQL subprograms Have block structures similar to anonymous blocks: – Optional declarative section (without DECLARE keyword) – Mandatory executable section – Optional section to handle exceptions 9-3 Copyright © 2004, Oracle. All rights reserved.
  4. 4. Differences Between Anonymous Blocks and Subprograms Anonymous Blocks Subprograms Unnamed PL/SQL blocks Named PL/SQL blocks Compiled every time Compiled only once Not stored in the database Stored in the database Cannot be invoked by They are named and therefore can other applications be invoked by other applications Do not return values Subprograms called functions must return values Cannot take parameters 9-4 Can take parameters Copyright © 2004, Oracle. All rights reserved.
  5. 5. Procedure: Syntax CREATE [OR REPLACE] PROCEDURE procedure_name [(argument1 [mode1] datatype1, argument2 [mode2] datatype2, . . .)] IS|AS procedure_body; 9-5 Copyright © 2004, Oracle. All rights reserved.
  6. 6. Procedure: Example ... CREATE TABLE dept AS SELECT * FROM departments; CREATE PROCEDURE add_dept IS dept_id dept.department_id%TYPE; dept_name dept.department_name%TYPE; BEGIN dept_id:=280; dept_name:='ST-Curriculum'; INSERT INTO dept(department_id,department_name) VALUES(dept_id,dept_name); DBMS_OUTPUT.PUT_LINE(' Inserted '|| SQL%ROWCOUNT ||' row '); END; / 9-6 Copyright © 2004, Oracle. All rights reserved.
  7. 7. Procedure: Example 9-7 Copyright © 2004, Oracle. All rights reserved.
  8. 8. Invoking the Procedure BEGIN add_dept; END; / SELECT department_id, department_name FROM dept WHERE department_id=280; 9-8 Copyright © 2004, Oracle. All rights reserved.
  9. 9. Function: Syntax CREATE [OR REPLACE] FUNCTION function_name [(argument1 [mode1] datatype1, argument2 [mode2] datatype2, . . .)] RETURN datatype IS|AS function_body; 9-9 Copyright © 2004, Oracle. All rights reserved.
  10. 10. Function: Example CREATE FUNCTION check_sal RETURN Boolean IS dept_id employees.department_id%TYPE; empno employees.employee_id%TYPE; sal employees.salary%TYPE; avg_sal employees.salary%TYPE; BEGIN empno:=205; SELECT salary,department_id INTO sal,dept_id FROM employees WHERE employee_id= empno; SELECT avg(salary) INTO avg_sal FROM employees WHERE department_id=dept_id; IF sal > avg_sal THEN RETURN TRUE; ELSE RETURN FALSE; END IF; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; / 9-10 Copyright © 2004, Oracle. All rights reserved.
  11. 11. Invoking the Function SET SERVEROUTPUT ON BEGIN IF (check_sal IS NULL) THEN DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception'); ELSIF (check_sal) THEN DBMS_OUTPUT.PUT_LINE('Salary > average'); ELSE DBMS_OUTPUT.PUT_LINE('Salary < average'); END IF; END; / 9-11 Copyright © 2004, Oracle. All rights reserved.
  12. 12. Passing Parameter to the Function DROP FUNCTION check_sal; / CREATE FUNCTION check_sal(empno employees.employee_id%TYPE) RETURN Boolean IS dept_id employees.department_id%TYPE; sal employees.salary%TYPE; avg_sal employees.salary%TYPE; BEGIN SELECT salary,department_id INTO sal,dept_id FROM employees WHERE employee_id=empno; SELECT avg(salary) INTO avg_sal FROM employees WHERE department_id=dept_id; IF sal > avg_sal THEN RETURN TRUE; ELSE RETURN FALSE; END IF; EXCEPTION ... ... 9-12 Copyright © 2004, Oracle. All rights reserved.
  13. 13. Invoking the Function with a Parameter BEGIN DBMS_OUTPUT.PUT_LINE('Checking for employee with id 205'); IF (check_sal(205) IS NULL) THEN DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception'); ELSIF (check_sal(205)) THEN DBMS_OUTPUT.PUT_LINE('Salary > average'); ELSE DBMS_OUTPUT.PUT_LINE('Salary < average'); END IF; DBMS_OUTPUT.PUT_LINE('Checking for employee with id 70'); IF (check_sal(70) IS NULL) THEN DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception'); ELSIF (check_sal(70)) THEN ... END IF; END; / PUT THE SCREENSHOT OF OUTPUT HERE 9-13 Copyright © 2004, Oracle. All rights reserved.
  14. 14. Summary In this lesson, you should have learned how to: • Create a simple procedure • Invoke the procedure from an anonymous block • Create a simple function • Create a simple function that accepts parameters • Invoke the function from an anonymous block 9-14 Copyright © 2004, Oracle. All rights reserved.
  15. 15. Practice 9: Overview This practice covers the following topics: • Converting an existing anonymous block to a procedure • Modifying the procedure to accept a parameter • Writing an anonymous block to invoke the procedure 9-15 Copyright © 2004, Oracle. All rights reserved.
  16. 16. Practice 9: Overview 9-16 Copyright © 2004, Oracle. All rights reserved.

×