Inner function : Code Block « PL SQL Programming « Oracle PL/SQL Tutorial

Oracle PL/SQL Tutorial
1. Introduction
2. Query Select
3. Set
4. Insert Update Delete
5. Sequences
6. Table
7. Table Joins
8. View
9. Index
10. SQL Data Types
11. Character String Functions
12. Aggregate Functions
13. Date Timestamp Functions
14. Numerical Math Functions
15. Conversion Functions
16. Analytical Functions
17. Miscellaneous Functions
18. Regular Expressions Functions
19. Statistical Functions
20. Linear Regression Functions
21. PL SQL Data Types
22. PL SQL Statements
23. PL SQL Operators
24. PL SQL Programming
25. Cursor
26. Collections
27. Function Procedure Packages
28. Trigger
29. SQL PLUS Session Environment
30. System Tables Data Dictionary
31. System Packages
32. Object Oriented
33. XML
34. Large Objects
35. Transaction
36. User Privilege
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL/SQL Tutorial » PL SQL Programming » Code Block 
24. 4. 6. Inner function
SQL>
SQL>
SQL>
SQL> create table employee (
  2  id                             number,
  3  employee_type_id                 number,
  4  external_id                    varchar2(30),
  5  first_name                     varchar2(30),
  6  middle_name                    varchar2(30),
  7  last_name                      varchar2(30),
  8  name                           varchar2(100),
  9  birth_date                     date  ,
 10  gender_id                      number );

Table created.

SQL>
SQL>
SQL>
SQL> create table gender (
  2  id                             number,
  3  code                           varchar2(30),
  4  description                    varchar2(80),
  5  active_date                    date          default SYSDATE  not null,
  6  inactive_date                  date );

Table created.

SQL>
SQL>
SQL>
SQL> insert into gender id, code, description values 1'F''Female' );

row created.

SQL> insert into gender id, code, description values 2'M''Male' );

row created.

SQL> insert into gender id, code, description values 3'U''Unknown' );

row created.

SQL>
SQL> create table employee_type (
  2  id                             number,
  3  code                           varchar2(30),
  4  description                    varchar2(80),
  5  active_date                    date          default SYSDATE  not null,
  6  inactive_date                  date );

Table created.

SQL>
SQL> insert into employee_type(id,code,description)values(1,'C','Contractor' );

row created.

SQL> insert into employee_type(id,code,description)values(2,'E','Employee' );

row created.

SQL> insert into employee_type(id,code,description)values(3,'U','Unknown' );

row created.

SQL>
SQL>
SQL>
SQL> set serveroutput on size 1000000;
SQL>
SQL> declare
  2  n_count                               number := 0;
  3
  4  FUNCTION add_worker(
  5  aiv_first_name                        employee.first_name%TYPE,
  6  aiv_middle_name                       employee.middle_name%TYPE,
  7  aiv_last_name                         employee.last_name%TYPE,
  8  aid_birth_date                        employee.birth_date%TYPE,
  9  aiv_gender_code                       gender.code%TYPE,
 10  aiv_employee_type_code                employee_type.code%TYPE)
 11  return                                number is
 12
 13  v_name                                employee.name%TYPE;
 14
 15  begin
 16    v_name        := rtrim(aiv_last_name||', '||aiv_first_name||' '||aiv_middle_name);
 17    begin
 18      insert into employee (
 19             id,
 20             employee_type_id,
 21             external_id,
 22             first_name,
 23             middle_name,
 24             last_name,
 25             name,
 26             birth_date,
 27             gender_id )
 28      select 1,
 29             myCursor.id,
 30             lpad(to_char(1)9'0'),
 31             aiv_first_name,
 32             aiv_middle_name,
 33             aiv_last_name,
 34             v_name,
 35             aid_birth_date,
 36             c2.id
 37      from   employee_type myCursor,
 38             gender c2
 39      where  myCursor.code = aiv_employee_type_code
 40      and    c2.code = aiv_gender_code
 41      and not exists (
 42        select 1
 43        from   employee x
 44        where  x.name       = v_name
 45        and    x.birth_date = aid_birth_date
 46        and    x.gender_id  = c2.id );
 47
 48      return sql%rowcount;
 49    exception
 50      when OTHERS then
 51        raise_application_error(-20001, SQLERRM||' oinsert employee'||' in add_worker');
 52    end;
 53  end add_worker;
 54
 55  begin
 56    n_count := n_count + add_worker('JOHN',   'J.', 'DOE', to_date('19800101', 'YYYYMMDD'), 'M''C');
 57    n_count := n_count + add_worker('JANE',   'J.', 'DOE', to_date('19800101', 'YYYYMMDD'), 'F''E');
 58    n_count := n_count + add_worker('JOHNNY', 'E.', 'DOE', to_date('19980101', 'YYYYMMDD'), 'M''E');
 59    n_count := n_count + add_worker('JANIE',  'E.', 'DOE', to_date('19980101', 'YYYYMMDD'), 'F''E');
 60    DBMS_OUTPUT.PUT_LINE(to_char(n_count)||' row(sinserted.');
 61  end;
 62  /
row(sinserted.

PL/SQL procedure successfully completed.

SQL>
SQL> drop table gender;

Table dropped.

SQL>
SQL> drop table employee;

Table dropped.

SQL>
SQL> drop table employee_type;

Table dropped.

SQL>
24. 4. Code Block
24. 4. 1. This is an anonymous procedure, so it has no name
24. 4. 2. A PL/SQL Block
24. 4. 3. Uses a PL/SQL Nested Block
24. 4. 4. Inline procedure
24. 4. 5. the forward slash on a line by itself says execute this procedure
24. 4. 6. Inner function
24. 4. 7. Select the first names for the Doe family from the Worker table.
24. 4. 8. Inner procedure in an anonymous function
24. 4. 9. Demonstrate nested PL/SQL blocks
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.