Objectives
•Base a data block on FROM clause sub query
•Base a data block on a stored procedure
•Return a REF cursor from a stored procedure
•Return a table of records from a stored procedure
•Select the appropriate data source for a
data block
Basing Data Block on a From Clause Query
SELECT deptno, Total_salary
FROM (SELECT deptno,
sum(sal) Total_salary
FROM emp
GROUP BY deptno)
WHERE total_salary>=8000
Why Form Clause Query?
Perform joins, lookups, and calculations on the server (thus avoiding
multiple network trips) without having to define a view every time.
•Improves developer productivity
•Reduces the burden on the DBA
•Improves performance
Basing a Data block on Stored Procedure for
Query Operation
Return data by way of:
•REF cursor
•A Table of records
Procedure
MARTIN
Empno Ename Job
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7566 JONES ANAGER
7654 SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
Querying
Emp No
Name
Job
Data Block
Basing a Data block on Stored Procedure for
DML Operations
•Return data by way of a table of records
Procedure
MARTIN
Empno Ename Job
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7566 JONES ANAGER
7654 SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
Updating
Emp No
Name
Job
Data Block
REF Cursor Procedure for Query
Define a package specification with:
•The objects returned by the REF cursor
•The REF cursor
•The query procedure: Data returned
through the first argument (IN OUT)
Define a package body: Write the code for
the query procedure
Table of Records Procedure for Query
Define a package specification with:
•The structure of each row of the table
•The table of records
•The query procedure: Data returned through the first argument (IN
OUT)
Define a package body: Write the code for the
query procedure
Table of Records Procedure for DML
Define a package specification with:
•The structure of each row of the table
•The table of records
•A procedure to insert rows
•A procedure to update rows
•A procedure to delete rows
•A procedure to lock rows
Define a package body by writing the code for
each DML procedure
Returning a Ref Cursor
Query
Fetched rows
REF cursor
REF cursor
Procedure
SQL
SELECT
21
3
4
Emp No
Name
Job
Returning Table of Records
Query
Table of Records
Cursor
Cursor
Procedure
Any
PL/SQL
Code
21
3
4
Emp No
Name
Job
Data Block Wizard
Use the Data Block Wizard to specify the following:
•Data source type
•Query procedure
•Insert procedure
•Update procedure
•Delete procedure
•Lock procedure
Data Source Guidelines
Base a data block on a FROM clause query to:
•Create a “dynamic” view
Base a data block on a stored procedure to:
•Increase control and security
•Specify a SELECT statement at run time
•Query or update multiple tables
•Perform complex computations
•Perform validation and DML on the
server side
Data Source Guidelines
Query
YES
YES
YES
YES
YES
YES
Data Source
Table
View
FROM Clause
Proc-Ref Cur
Proc-Table Rec
Transac. Trigger
DML
YES
YES
NO
NO
YES
YES
Summary
•Base data blocks on new data sources:
–FROM clause query (query only)
–Stored procedure (query and DML)
•Exploit features of stored procedures :
–REF cursors
limited to a single SELECT statement
–Table of records
can be a complex procedure