2. 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
4. 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
5. 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
6. 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
7. 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
8. 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
9. 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
10. 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
11. Returning a Ref Cursor
Query
Fetched rows
REF cursor
REF cursor
Procedure
SQL
SELECT
21
3
4
Emp No
Name
Job
12. Returning Table of Records
Query
Table of Records
Cursor
Cursor
Procedure
Any
PL/SQL
Code
21
3
4
Emp No
Name
Job
13. 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
14. 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
16. 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
Notas del editor
Queries with Stored Procedure Returning a REF Cursor
Executing queries using a stored procedure that returns a REF cursor takes
One round trip to execute the stored procedure(1,2), plus one round trip
(3,4) per array size of rows returned.
Performance Implications
The no of records fetched depends on the Array Fetch property value
Queries or DML with Stored Procedure Using PL/SQL Tables of Records
Executing queries or DML with stored procedures using PL/SQL tables of
Records takes one round trip to execute the stored procedure (1,2) plus one
Round trip (3,4) for all rows.
Performance Implications
All records are fetched.