You can define user-specific database functions. In an SQL statement, you can then use these user-defined database functions in the same way as any other predefined functions.
A database function can contain several SQL statements. A series of control structures are also available for the application programmer. You can program loops or branches, for example, within a database function.
Database functions are specialized database procedures that contain any number of input parameters but only one output parameter. The output parameter represents the result of the database function.
To create a database function, use the CREATE FUNCTION statement.
CREATE FUNCTION
avgprice (zip CHAR(5)) RETURNS FIXED(6,2) AS
VAR sum FIXED(10,2); price FIXED(6,2);
hotels INTEGER; avg_price FIXED(6,2);
TRY
SET sum = 0; SET hotels = 0;
SELECT price FROM mona.room,mona.hotel WHERE zip = :zip AND
room.hno = hotel.hno AND type = 'single';
WHILE $rc = 0 DO BEGIN
FETCH INTO :price;
SET sum = sum + price;
SET hotels = hotels + 1;
END;
CATCH
IF $rc <> 100 THEN STOP ($rc, 'unexpected error');
IF hotels > 0 THEN RETURN sum/hotels
ELSE RETURN NULL;
The avgprice database function determines the average price for single rooms in hotels that are located within the specified zip code range.
· The input parameter is the zip code zip; the data type of the output value is specified after the keyword RETURNS.
· The SQL statements (the SELECT statement, for example) and control structures (WHILE statement and an IF statement, for example) that are required for executing the desired function are defined after the keyword AS.
· The variables required within the database function are defined after the keyword VAR.
· Troubleshooting: if an SQL error occurs in the statement list between TRY and CATCH, the system branches directly to the statement that follows CATCH. The actual troubleshooting routine can be programmed in this statement.
· SET can be used to assign values to the sum and hotels variables.
· The tables in the SELECT statement must always be complete, that is, with the owner specified. Make sure that the zip code is transferred as the parameter: :zip.
· The WHILE statement enables statements to be repeated in response to certain conditions. The statement is executed as long as the specified search condition is true.
· The $rc variable returns a numeric error code after the SELECT statement has been executed. The value 0 means that the SELECT statement was successfully executed.
· The IF statement first evaluates the search condition. If this is fulfilled, the statement specified in the THEN branch is executed. Otherwise, the statement in the ELSE branch (if defined) is executed.
· The output parameter is defined after the keyword RETURN.
See also:
CREATE FUNCTION Statement (create_function_statement)
To call up a database function, follow the procedure for calling up a predefined function.
If the hotel table is filled with the data specified in HOTELDB_SIMPLE, you can test the following SELECT statement:
SELECT hno, avgprice(zip) avgprice FROM hotel WHERE hno < 100
Result
HNO |
AVGPRICE |
10 |
135 |
20 |
70 |
30 |
45 |
40 |
87.5 |
50 |
105 |
60 |
120 |
70 |
115 |
80 |
87.5 |
90 |
90 |
See also:
Database Function Call (dbfunction_call)
To drop a database function, use the DROP FUNCTION statement.
DROP FUNCTION avgprice
You can use this SQL statement to drop the database function definition.
See also:
DROP FUNCTION Statement (drop_function_statement)