Executing Routines
You can execute an SPL routine or external routine in several ways:
An external routine is a routine written in C or some other external language.
The EXECUTE Statements
You can use EXECUTE PROCEDURE or EXECUTE FUNCTION to execute an SPL routine or external routine. In general, it is best to use EXECUTE PROCEDURE with procedures and EXECUTE FUNCTION with functions.
You can issue EXECUTE PROCEDURE and EXECUTE FUNCTION statements as standalone statements from DB-Access or from within an SPL routine or external routine.
How to Use the Statements
If the routine name is unique within the database, and if it does not require arguments, you can execute it by entering just its name and parentheses after EXECUTE PROCEDURE as Figure 14-96 shows.
Figure 14-96
The INTO clause is never present when you invoke a procedure with the EXECUTE statement because a procedure does not return a value.
If the routine expects arguments, or if the routine name is not unique in the database, you must enter the argument values within parentheses, as Figure 14-97 shows.
Figure 14-97
If the database has more than one function of the same name, Universal Server locates the right function based on the data types of the arguments. For example, the statement in Figure 14-97 supplies INTEGER and REAL values as arguments, so the scale_rectangles() function that accepts those data types is executed.
Notice that the statement in Figure 14-97 executes a function. Because a function returns a value, EXECUTE FUNCTION uses an INTO clause that specifies a variable where the return value is stored. The INTO clause must always be present when you use an EXECUTE statement to execute a function.
Remember that the parameter list of an SPL routine always has parameter names, as well as data types. When you execute the routine, the parameter names are optional. However, if you pass arguments by name (instead of just by value) to EXECUTE PROCEDURE or EXECUTE FUNCTION, as in Figure 14-98, Universal Server resolves the routine-by-routine name and arguments only, a process known as partial routine resolution.
Figure 14-98
You can also execute an SPL routine stored on another server by adding a qualified routine name to the statement, that is, a name in the form database@dbserver:owner_name.routine_name, as in Figure 14-99.
Figure 14-99
When you execute a routine remotely, the owner_name in the qualified routine name is optional.
Using the CALL Statement
You can call an SPL routine or an external routine from an SPL routine using the CALL statement. CALL executes both procedures and functions. If you use CALL to execute a function, add a RETURNING clause and the name of an SPL variable (or variables) that will receive the value (or values) the function returns.
Suppose, for example, that you want the scale_rectangles function to call an external function that calculates the area of the rectangle and then returns the area, along with the rectangle description.
Figure 14-100
The SPL function in Figure 14-100 uses a CALL statement that executes the external function area(). The value area() returns is stored in a and returned to the calling routine by the RETURN statement.
In this example, area() is an external function, but you can use CALL in the same manner with an SPL function.
Executing Routines in Expressions
Just as with built-in functions, you can execute SPL routines (and external routines from SPL routines) by using them in expressions in SQL and SPL statements. A routine used in an expression is usually a function because it returns a value to the rest of the statement.
For example, you might execute a function by a LET statement that assigns the return value to a variable. The statements in Figure 14-101 perform the same task. They execute an external function within an SPL routine and assign the return value to the variable a.
Figure 14-101
You can also execute an SPL routine from an SQL statement as Figure 14-102 shows. Suppose you have written an SPL function, increase_by_pct, which increases a given price by a given percentage. Once you write an SPL routine, it is available for use in any other SPL routine.
Figure 14-102
The example in Figure 14-102 selects the price column of a specified row of inventory and uses the value as an argument to the SPL function increase_by_pct. The function then returns the new value of price, increased by 20 percent, in the variable p.
Executing Cursor Functions from an SPL Routine
A cursor function is a user-defined function that returns one or more rows of data and therefore requires a cursor to execute. A cursor function can be either of the following functions:
The behavior of a cursor function is the same whether the function is an SPL function or an external function. However, an SPL cursor function can return more than one value per iteration whereas an external cursor function (iterator function) can return only one value per iteration.
To execute a cursor function from an SPL routine, you must include the function in a FOREACH loop of an SPL routine. The following examples show different ways to execute a cursor function in a FOREACH loop:
Dynamic Routine-Name Specification
Dynamic routine-name specification allows you to execute an SPL routine from another SPL routine by building the name of the called routine within the calling routine. Dynamic routine-name specification simplifies the writing of an SPL routine that calls another SPL routine whose name is not known until runtime. In Universal Server, you can specify an SPL variable instead of the explicit name of a SPL routine in the EXECUTE PROCEDURE or EXECUTE FUNCTION statement.
In Figure 14-103, the SPL procedure company_proc updates a large company sales table and then assigns an SPL variable named salesperson_proc to hold the dynamically created name of an SPL procedure that updates another, smaller table that contains the monthly sales of an individual salesperson.
Figure 14-103
In Figure 14-103 the procedure company _proc accepts five arguments and inserts them into company_tbl. Then the LET statement uses various values and the concatenation operator || to generate the name of another SPL procedure to execute. In the LET statement:
Therefore, if a salesperson named Bill makes a sale in July 1996, company_proc inserts a record in company_tbl and executes the SPL procedure bill.tbl07_1996_proc, which updates a smaller table that contains the monthly sales of an individual salesperson.
Rules for Dynamic Routine-Name Specification
You must define the SPL variable that holds the name of the dynamically executed SPL routine as CHAR, VARCHAR, NCHAR, or NVARCHAR type. You must also give the SPL variable a valid and non-null name.
The SPL routine that the dynamic routine-name specification identifies must exist before it can be executed. If you assign the SPL variable the name of a valid SPL routine, the EXECUTE PROCEDURE or EXECUTE FUNCTION statement executes the routine whose name is contained in the variable, even if a built-in function of the same name exists.
In an EXECUTE PROCEDURE or EXECUTE FUNCTION statement, you cannot use two SPL variables to create a variable name in the form owner.routine_name. However, you can use an SPL variable that contains a fully qualified routine name, for example, bill.proc1. Figure 14-104 shows both cases.
Figure 14-104
|