Benefits of Using Oracle Forms' Stored Procedure Data Blocks

By Art Collins, OCP Introduction

Friday, December 23, 2011

With all the rich features Oracle Forms offer a developer, one of the least understood and least utilized functionality is the use of stored procedure data blocks. This functionality is really not new. It was first introduced in Forms version 5.0, but has received very little press coverage so far. This article will shed some light on stored procedure data blocks, describing why they are useful, and how to implement them.
View the source code for this article The Problem

The traditional method of creating a data block has always been a simple but constrained process. Prior to version 5.0, the only way to generate one was to base it from a single table. This technique does not meet the real-world needs of relational database systems, since most forms require data from multiple tables (often requiring complex joins). Experienced developers discovered two basic ways to circumvent this problem: 1. Create a database view from these complex joins, or 2. Write queries in post-query trigger to fetch the remaining data not already retrieved from the base table. These two solutions have some serious drawbacks. First, creating a view for each form is not always possible. Since a view is basically a SQL statement, there will be instances where it's not possible to retrieve all the data in one well-behaved optimized SQL (for example, when extensive data processing needs to be done to compute columns). Data modification is also an issue because updatable views have restrictions. As a result of these constraints, the second solution of writing post-query trigger is a more common one. The biggest drawback to this approach is performance. Within the trigger, each query fires separately for every row, causing excessive network traffic between the client and server computers. Illustration: Let's take as an example a table data block with 2 additional queries in the postquery trigger. In a query where 5 rows are being retrieved into the form, a post-query trigger will fire 5 times (one for each row), and each trigger performs the 2 queries. That is 3 fetches per row, which result in 15 fetches for 5 rows of data. No wonder most complex forms are slow to fetch data. There are a lot of queries going on there!
A Viable Solution

All this extra work may seem rather tedious. This approach limits Forms data access to through these procedures and eliminates the need of writing SQL in Forms (with the exception of LOVs. Example A form needs to be created to query and manipulate (update. but the following benefits illustrate why stored procedure data blocks are a better choice. Since the entire query is performed in the database. populating the result in a PL/SQL table. Like its name implies. It is important to keep in mind that this is only a single approach. At their most extreme. the data block is now based on a stored procedure in the database. Performance. The basic concept involves writing a database procedure to do the query (or queries). instead of basing a data block from a single table. delete. Reduced Network Traffic. or insert procedure in the database. there is another alternative that is much cleaner and more efficient. Sharing Across Forms. y y y y Increased Security and Query Standards.With the release of Forms version 5. and delete) employee data. Performance. and insert capabilities. This increases reusability and improves application maintenance. To provide update. using a stored procedure data block may be the easiest and most efficient solution. the developer can use stored procedure data blocks to create a 'middle tier' or 'data access layer' by writing sets of procedures to access and manipulate the data. which still require SQL definition). it may be easily optimized. But in situations where a data block will fetch and modify information from multiple tables. and passing it to Forms so it can populate the data block. a stored procedure data block. Thus. which may or may not be applicable for all programming needs. Since Forms is just sending or receiving a PL/SQL table from the database. and overall performance is greatly increased. insert. which needs to have the ability to query. update. This data comes from two tables with a 1 to 1 relationship. and delete data. Combine this with the efficiency gained by having only one 'round trip' data transfer between the database and Forms. The same stored procedure may be shared across different Form modules. Employee table Name Emp_id First_name Last_name Null? not null not null not null Type nnumber(10) varchar2(20) varchar2(20) Employee_confidential table Name Null? Emp_id not null Type number(10) . network traffic is reduced. Forms pass a PL/SQL table to an update. insert. This is the use of stored procedure data block. the opposite happens. The procedure loops through all the rows in the PL/SQL table and applies the appropriate changes to the tables. Performance. delete. There is no post-query trigger code to deal with. requires separate procedures (all implemented in a package) to perform each one of these functions.

last_name employee. type emp_tbl is table of emp_rec index by binary_integer. Simply declare an emp_tbl argument as IN OUT. end loop.ssn%type.emp_id and e. e. begin for rec in emp_cur(p_emp_id) loop -.first_name%type. e. end.emp_id. procedure do_query(p_emp_table IN OUT emp_tbl. This is the parameter that will get passed from the database to Forms.populate a PL/SQL table with a record p_emp_table(idx) := rec. p_emp_id is needed because the query looks for a specific employee id.salary%type).last_name%type. Notice that emp_rec is nothing more than the columns which need to be queried. The procedure then populates p_emp_table PL/SQL table and passes it back to the form for .last_name.salary from employee e. This PL/SQL table will be populated by the query and passed back to the form.Ssn Salary not null not null varchar2(9) number(10) To create a procedure to perform the query.emp_id%type. That is it for the query! do_query procedure is called from Forms with the specified emp_id. c. Since it is easier to group all these procedures into a package. first_name employee. c. salary employee_confidential.first_name. In this particular case. a PL/SQL table needs to be defined.emp_id = p_id. Now that the PL/SQL table type is defined.increment index idx := idx + 1.emp_id = c.emp_id%type) is select e. Add additional parameters as needed to perform the query. employee_confidential c where e. These columns are what will appear on the form. p_emp_id IN employee.emp_id%type) is cursor emp_cur (p_id employee. we will do so as follows.ssn. idx number := 1. creating the query procedure is a simple task. ssn employee_confidential. -. emp_tbl is the PL/SQL table made up of emp_recrecords. Create or replace package employee_pkg is type emp_rec is record (emp_id employee.

Simply open up the Data Block Wizard and select Stored Procedure. The Wizard will prompt for the stored procedures one at a time (query. insert.emp_id. If the data block needs data manipulation capability.salary). Here you may specify binding variables for these parameters.emp_id. The logic is the same as the update or delete procedure. it's time to create a Form module to access them. p_emp_table(idx). update. p_emp_table(idx). When Forms locates the procedure. if an update or delete procedure is created. it automatically retrieves the column definitions from the PL/SQL table as well as any procedure parameters. and lock). a PL/SQL table is sent to the insert stored procedure where the actual insert queries are issued. Its implementation is relatively straightforward and an example code is provided in the complete listing below.ssn. update. There is another method of writing a query procedure using cursor variables instead of a PL/SQL table. delete. One final note. The insert procedure requires putting records into both employee andemployee_confidential tables (remember that it's a 1 to 1 relationship).count.. for update nowait. procedure do_insert(p_emp_table IN OUT emp_tbl) is numOfRows number. When the forms 'Insert-Procedure' trigger is fired after the user inserts a record. salary) values (p_emp_table(idx). end . Once the stored procedures are successfully compiled in Oracle. end loop.display. last_name) values (p_emp_table(idx).. which requires looping through all the records in the PL/SQL table and issue a select . insert into employee_confidential (emp_id. begin numOfRows := p_emp_table. a lock procedure needs to be created as well. for idx in 1 . This procedure is called to lock a record in the database when the user attempts to modify it. statement for each affected table. and delete procedures need to be created. numOfRows loop insert into employee (emp_id. it will automatically generate triggers . ssn. p_emp_table(idx).first_name. The procedure simply loops through all the records in the PL/SQL and performs inserts into the two tables.. Once the form is compiled.last_name). then insert. first_name. Code for update and delete procedures are very similar to this insert except update and delete statements would be issued. p_emp_table(idx).

the first step is to write the stored procedures to access or manipulate your data. In summary. all the rest are just slight variations. have fun. Modifications may be done through the following block properties: y y Query Data Source Columns . recompile the form. So go ahead. The second step is to create a data block that references these procedures. play with it. The foregoing may seem quite complicated at first.lists the column definition of the PL/SQL table Query Data Source Arguments . and changes should be reflected in the data block triggers. You will see that once you design one stored procedure block. good luck! . and most of all. but it is really straightforward after you try it.depending on what stored procedures were used. They are: y y y y y Query-procedure Insert-procedure Update-procedure Delete-procedure Lock-procedure Do not attempt to modify code in these triggers because they are automatically generated every time Form compiles.lists the parameters to the query procedure Once these properties are modified.

Sign up to vote on this title
UsefulNot useful