PL/SQL   «Prev  Next»

Lesson 6Creating a PL/SQL block to query object tables
ObjectiveWrite a PL/SQL block to retrieve data from an object table.

Creating PL/SQL Block to query Object Tables

In this lesson, we will look at a PL/SQL block that uses a cursor to query an object table.
The following SlideShow shows an example of such a PL/SQL block:

1) Within the DECLARE section, the cursor and the variables are defined.
1) Within the DECLARE section, the cursor and the variables are defined.. Defining a PL/SQL cursor that queries an object table is very similar to defining a PL/SQL cursor that queries a relational table.

2) For signals the beginning of the FOR Loop
FOR see_all_date IN get_all_date LOOP
  dbms_output.put_line( The Customer
  '|| see_all_dat.firstname||'
  '||see_all_data.Lastname|| 'reside in 
  '||||' ,
For signals the beginning of the FOR Loop. After retrieving

3) END signals closing of the cursor after data processing is complete
END signals closing of the cursor after data processing is complete

PL/SQL Cursor Block

Question: How do PL/SQL Cursor Blocks make it easy to query and change the contents of Oracle tables?
PL/SQL is appropriate or required for querying object tables in Oracle in certain situations, such as:
  1. When complex business logic is involved: If the querying of an object table involves complex business logic that cannot be expressed using standard SQL queries, PL/SQL may be required. PL/SQL allows developers to write complex algorithms and manipulate data in ways that would be difficult or impossible to do using SQL alone.
  2. When using user-defined types: If an object table contains user-defined types, PL/SQL may be required to manipulate the data. This is because SQL has limited support for user-defined types, and PL/SQL provides more flexibility and control over how the data is accessed and manipulated.
  3. When using advanced features: Oracle provides advanced features such as pipelined table functions, which enable developers to process data in a streaming fashion, and row-level security, which restricts access to specific rows in a table. These features often require the use of PL/SQL code to implement.
  4. When using dynamic SQL: If the query against an object table needs to be constructed dynamically at runtime, PL/SQL may be required. This is because SQL does not provide a way to dynamically generate queries, and PL/SQL must be used to generate the necessary SQL statements.
In summary, PL/SQL may be appropriate or required for querying object tables in Oracle when the querying involves complex business logic, user-defined types, advanced features, or dynamic SQL.
The central purpose of the Oracle PL/SQL language is to make it as easy and efficient as possible to query and change the contents of tables in a database. You must use the SQL language to access tables, and each time you do so, you use a cursor to execute your task.
Question:What is a cursor?
A cursor
  1. is a pointer to a private SQL area that stores information about the processing of a SELECT or
  2. (DML)data manipulation language statement (INSERT, UPDATE, DELETE, or MERGE).
Cursor management of DML statements is handled by Oracle Database, but PL/SQL offers several ways to define and manipulate cursors to execute SELECT statements.

We will now look at an example that uses a cursor to extend the example from the previous lesson. In this example, we define a cursor based on the SALE_HEADER table. This cursor finds a particular record (where sale_id = 34) and updates the record within the PL/SQL block.

the_customer customer_type;
id_for_sale number(10);
CURSOR cur_cust_type IS 
SELECT REF(sh) refer, sh.sale_id 
INTO   the_customer, id_for_sale
FROM   sale_header sh
FOR UPDATE OF sh.tax_amount;
    FOR get_cust_type IN cur_cust_type LOOP
     IF get_cust_type.sale_id = 34 THEN
UPDATE sale_header sh
SET tax_amount = 200
WHERE sale_id = 34
AND REF(sh) = get_cust_type.refer;
     END IF;

The next lesson wraps up this module.
Click the link below to learn about how to build a cursor block that queries multiple object tables.
Build PL/SQL block

Native compilation of PL/SQL

By default, PL/SQL code is compiled and stored in the form of byte code ready for execution. During the execution process, this byte code is interpreted, a process which requires time and resources. The process of native compilation converts PL/SQL stored procedures to Pro*C, which is then compiled to native code shared libraries, resulting in performance increases for the procedural code. The extent of the performance increase depends on the content of the PL/SQL, with the best results shown in code containing loops, logic, mathematical operations and fewer database operations (SQL).
The setup required for native compilation depends on the version of Oracle being used. In Oracle 9i several parameters must be set and on some platforms the associated makefile may need adjustment, whereas Oracle 10g has made several parameters obsolete and the makefile rarely needs modification.

-- Oracle 9i setup.

ALTER SYSTEM SET plsql_native_make_utility = 'make';
 ALTER SYSTEM SET plsql_native_make_file_name = 
 ALTER SYSTEM SET plsql_native_library_dir = '/u01/oradata/DB9I/native';

-- Oracle 10g setup.
 ALTER SYSTEM SET plsql_native_library_dir = '/u01/oradata/DB10G/native'

One example of how to build your PL/SQL block looks like the following:
 prod_id number(10);
 SELECT product_id into prod_id
 FROM product_obj_table
 WHERE product_name = 'Dog';
 UPDATE pet_care_log_obj_table
 SET  log_text = 'This dog must be given special care 
  and attention since he is partially blind.'
 WHERE product_id = (SELECT REF(prod) FROM product_obj_table
  prod WHERE product_id = prod_id);