![]() |
|
You can execute an SPL routine or external routine in any of the following ways:
An external routine is a routine written in C or some other external language.
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.
Tip: For backward compatibility, the EXECUTE PROCEDURE statement allows you to use an SPL function name and an INTO clause to return values. However, Informix recommends that you use EXECUTE PROCEDURE only with procedures and EXECUTE FUNCTION only with functions.
You can issue EXECUTE PROCEDURE and EXECUTE FUNCTION statements as stand-alone statements from DB-Access or the Relational Object Manager from within an SPL routine or external routine.
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 10-96 shows.
Figure 10-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, you must enter the argument values within parentheses, as Figure 10-97 shows.
Figure 10-97
The statement in Figure 10-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.
If the database has more than one procedure or function of the same name, Dynamic Server locates the right function based on the data types of the arguments. For example, the statement in Figure 10-97 supplies INTEGER and REAL values as arguments, so if your database contains multiple routines named scale_rectangles(), the database server executes only the scale_rectangles() function that accepts INTEGER and REAL data types.
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 10-98, Dynamic Server resolves the routine-by-routine name and arguments only, a process known as partial routine resolution.
Figure 10-98
You can also execute an SPL routine stored on another database 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 10-99.
Figure 10-99
When you execute a routine remotely, the owner_name in the qualified routine name is optional.
You can call an SPL routine or an external routine from an SPL routine using the CALL statement. CALL can execute 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 with the rectangle description, as in Figure 10-100.
Figure 10-100
The SPL function in Figure 10-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.
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 10-101 perform the same task. They execute an external function within an SPL routine and assign the return value to the variable a.
Figure 10-101
You can also execute an SPL routine from an SQL statement, as Figure 10-102 shows. Suppose you write an SPL function, increase_by_pct, which increases a given price by a given percentage. After you write an SPL routine, it is available for use in any other SPL routine.
Figure 10-102
The example in Figure 10-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.
You can use a RETURN statement to execute any external function from within an SPL routine. Figure 10-103 shows an external function that is used in the RETURN statement of an SPL program.
Figure 10-103
When you execute the spl_func() function, the c_func() function is invoked, and the SPL function returns the value that the external function returns.
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 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 how you can write an SPL routine that calls another SPL routine whose name is not known until runtime. The database server lets you specify an SPL variable instead of the explicit name of an SPL routine in the EXECUTE PROCEDURE or EXECUTE FUNCTION statement.
In Figure 10-104 on page 10-77, 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 10-104
In Figure 10-104, 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 1998, company_proc inserts a record in company_tbl and executes the SPL procedure bill.tbl07_1998_proc, which updates a smaller table that contains the monthly sales of an individual salesperson.
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 10-105 shows both cases.
Figure 10-105