|
For an introduction on how to handle unknown return values from a user-defined function, see Determining Return Values Dynamically. This section describes how to use an sqlda structure to save values that a dynamically executed user-defined function returns.
A noncursor function returns only one row of return values to the application. Use the EXECUTE...INTO DESCRIPTOR statement to execute the function and save the return value(s) in an sqlda structure.
An external function that is not explicitly defined as an iterator function returns only a single row of data. Therefore, you can use EXECUTE...INTO DESCRIPTOR to execute most external functions dynamically and save their return values into an sqlda structure. This single row of data consists of only one value because an external function can only return a single value. The sqlda structure contains only one item descriptor with the single return value.
An SPL function whose RETURN statement does not include the WITH RESUME keywords returns only a single row of data. Therefore, you can use EXECUTE...INTO DESCRIPTOR to execute most SPL functions dynamically and save their return values into an sqlda structure. An SPL function can return one or more values at one time so the sqlda structure contains one or more item descriptors.
Warning: Because you usually do not know the number of returned rows that a user-defined function returns, you cannot guarantee that only one row will be returned. If you do not use a cursor to execute cursor function, ESQL/C generates a runtime error. Therefore, it is a good practice to always associate a user-defined function with a function cursor.
Executing a Cursor Function
A cursor function can return one or more rows of return values to the application. To execute a cursor function, you must associate the EXECUTE FUNCTION statement with a function cursor and use the FETCH...INTO DESCRIPTOR statement to save the return value(s) in an sqlda structure.
Only an external function that is defined as an iterator function can return more than one row of data. Therefore, you must define a function cursor to execute an iterator function dynamically. Each row of data consists of only one value because an external function can only return a single value. For each row, the sqlda structure contains only one sqlvar_struct structure with the single return value.
An SPL function whose RETURN statement includes the WITH RESUME keywords returns can return one or more row of data. Therefore, you must define a function cursor to execute these SPL functions dynamically. Each row of data can consist of one or more values because an SPL function can return one or more values at one time. For each row, the sqlda structure contains an sqlvar_struct structure for each return value.