Scalable Database Server, HiRDB Version 8 Description
When a set of operations on a database is defined as a procedure, it is called a stored procedure; when a set of operations on a database is defined as a function, it is called a stored function.
Defining a stored procedure or a stored function generates an SQL object that codes an access procedure. The resulting stored procedure or stored function, together with its definition information, is stored in the database. Processing procedures for stored procedures or stored functions can be coded in either SQL or Java. A procedure that is coded in SQL is called an SQL stored procedure or an SQL stored function; a procedure that is coded in Java is called a Java stored procedure or a Java stored function. For details about Java stored procedures and Java stored functions, see Section 5.4 Java stored procedures and Java stored functions.
A stored procedure may or may not have input, output, or input/output parameters; it is called by an SQL CALL statement. A stored function may or may not have input parameters; it is able to return a return value, and thus can be called as a value expression in an SQL. It should be noted, however, that a stored function can be used only for processing data; it cannot be used for accessing tables in the database.
Explained as follows are the types of jobs for which a stored procedure may be useful. For example, a product management job might involve the processing described as follows for analyzing the sales status of a product.
This is accomplished through the multiple database access processes described as follows.
When a series of database accessing processes such as the job shown here is registered at the database side rather than at the UAP side, the processes can be called for use from multiple UAPs. Figure 5-1 shows jobs to which a stored procedure can be applied.
Figure 5-1 Jobs to which a stored procedure can be applied
In the database access process shown in Figure 5-1, the total number of units ordered during June 2000 is determined for each product from the PRODUCT ORDERS TABLE, which has PRODUCT NO, ORDER QUANTITY, and RECORDING DATE as its columns. The results are incorporated into the MASTER ORDERS TABLE, which has PRODUCT NO and CUMULATIVE ORDER QUANTITY as its columns. In this case, the database access process described as follows can be defined as a stored procedure and registered in the database.
A UAP can perform the product management job shown in Figure 5-1 by simply calling the stored procedure with the argument described as follows specified.
Figure 5-2 shows how a stored procedure is used.
Figure 5-2 Stored procedure usage
This illustrates how, when a stored procedure is used, it is possible to register a database access process at the database side, thus turning the database access process into a component.
Even when the database access process is changed, all that has to be done is to change the stored procedure; there is no need to change the UAP, thus reducing the number of steps required for UAP development.
In order to execute multiple SQL statements, an application typically has to access the database as many times as there are SQL statements to be executed. By contrast, storing multiple SQL statements to be executed in the database as a stored procedure means that the multiple SQL statements can be executed with one call of the stored procedure and only one access to the database. This greatly reduces communication processing associated with the passing of data between the HiRDB server and HiRDB client applications, as well as the overhead associated with the parsing of SQL statements by the front-end server. Figure 5-3 shows the communications processing for an SQL stored procedure.
Figure 5-3 Communications processing for an SQL stored procedure
With a stored function, the user can define data processing in a database as an arbitrary function, using conditional branching (IF statement) and a routine control SQL such as SQL repetition (WHILE statement). Consequently, the user can turn data processing into a component. When a plug-in is used, the function provided by the plug-in is registered as a stored function in the database.
To use stored procedures or stored functions, the following types of RDAREAs must be created:
For details about creating RDAREAs for storing stored procedures and stored functions, see the HiRDB Version 8 System Operation Guide.
Explained as follows are the methods of calling a stored procedure or a stored function. For details about creating a UAP for calling stored procedures and stored functions, see the HiRDB Version 8 UAP Development Guide.
Multiple stored functions with the same name can be defined, as long as they have different numbers of parameters and their data types are different. Stored functions with the same name are mutually overloaded. Because the overload function can be used to assign the same name to multiple functions with different parameter data types, it is possible to standardize the names of functions that have the same function. When a stored function is invoked, those facilities described as follows become the candidates for execution from among the facilities with the specified name and the same number of parameters as the number of specified arguments:
For details about the rules for selecting the function to be invoked, see the HiRDB Version 8 SQL Reference.
Explained as follows are the SQL procedures and the definition of user-defined functions. For details about SQL procedures and the definition of user-defined functions, see the HiRDB Version 8 UAP Development Guide.
Procedures, user-defined functions, and system-defined functions are referred to collectively as routines.
When an SQL that defines a procedure or a user-defined function is executed by database definition (pddef), the procedure or user-defined function is compiled automatically, thus creating an SQL object. Moreover, the definition source of the SQL and the SQL object are stored in a data dictionary LOB RDAREA. This registers the stored procedure or stored function into the database. For details about registration of stored procedures and stored functions into the database, see the HiRDB Version 8 System Operation Guide.
If the definition of a table or index is changed or a stored procedure is deleted by the DROP PROCEDURE statement, you need to re-create the voided stored procedure by executing the ALTER PROCEDURE statement. Similarly, if a stored function is deleted by the DROP FUNCTION statement, you need to re-create the voided stored function by executing the ALTER ROUTINE statement. For details about re-creating a stored procedure or a stored function, see the HiRDB Version 8 System Operation Guide.
All Rights Reserved. Copyright (C) 2007, Hitachi, Ltd.