A user-defined function is a function that you write in SPL or in a language external to the database, such as C or Java.
User-Defined Functions: .-,-------------------------------------. V (1) | |--function--(----+--------------+--| Expression |------+-------> '-parameter--=-' >--+---------------------------------------------------------+--> | (2) (3) | '--------,--| Statement-Local Variable Declaration |------' >--)------------------------------------------------------------|
Element | Description | Restrictions | Syntax |
---|---|---|---|
function | Name of the function | Function must exist | Database Object Name, p. Database Object Name |
parameter | Name of an argument that was declared in a CREATE FUNCTION statement | If you use the parameter = option for any argument in the called function, you must use it for all arguments | Identifier,
p. Identifier |
You can call user-defined functions within SQL statements. Unlike built-in functions, user-defined functions can only be used by the creator of the function, the DBA, and the users who have been granted the Execute privilege on the function. For more information, see GRANT.
The following examples show some user-defined function expressions. The first example omits the parameter option when it lists the function argument:
read_address('Miller')
This second example uses the parameter option to specify the argument value:
read_address(lastname = 'Miller')
When you use the parameter option, the parameter name must match the name of the corresponding parameter in the function registration. For example, the preceding example assumes that the read_address( ) function had been registered as follows:
CREATE FUNCTION read_address(lastname CHAR(20)) RETURNING address_t ... ;
In Dynamic Server, a statement-local variable (SLV) enables you to transmit a value from a user-defined function call to another part of the SQL statement.
For information on how to write a UDR with OUT or INOUT parameters, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
For more information, see Specifying INOUT Parameters for a User-Defined Routine (IDS), and Specifying OUT Parameters for User-Defined Routines.
The call to the user-defined function must be made within a WHERE clause. For information about the syntax to declare the SLV, see Statement-Local Variable Declaration (IDS).
Once the call to the user-defined function has initialized the SLV, you can use this value in other parts of the SQL statement. For information about the use of an SLV within an SQL statement, see Statement-Local Variable Expressions (IDS).
The Statement-Local Variable Declaration declares a statement-local variable (SLV) in a call to a user-defined function that defines one or more OUT or INOUT parameters.
Statement-Local Variable Declaration: (1) |--slv_name--#--+-| Built-In Data Type |------+-----------------| +-opaque_data_type------------+ +-distinct_data_type----------+ | (2) | '-| Complex Data Type |-------'
Element | Description | Restrictions | Syntax |
---|---|---|---|
distinct_data_type | Name of a distinct data type | The distinct data type must already exist in the database | Identifier, p. Identifier |
opaque_data_type | Name of an opaque data type | The opaque data type must already exist in the database | Identifier, p. Identifier |
slv_name | Name of a statement local variable you are defining | The slv_name is valid only for the life of the statement, and must be unique within the statement | Identifier, p. Identifier |
You declare an SLV in a user-defined function call so that a user-defined function can assign the value of its OUT or INOUT parameter to the SLV. The UDF must be invoked in the WHERE clause of the SQL statement. For example, if you register a function with the following CREATE FUNCTION statement, you can use its y parameter as an SLV in a WHERE clause:
CREATE FUNCTION find_location(a FLOAT, b FLOAT, OUT y INTEGER) RETURNING VARCHAR(20) EXTERNAL NAME "/usr/lib/local/find.so" LANGUAGE C
In this example, find_location( ) accepts two FLOAT values that represent a latitude and a longitude and return the name of the nearest city with an extra value of type INTEGER that represents the population rank of the city.
You can now call find_location( ) in a WHERE clause:
SELECT zip_code_t FROM address WHERE address.city = find_location(32.1, 35.7, rank # INT) AND rank < 101;
The function expression passes two FLOAT values to find_location( ) and declares an SLV named rank of type INT. In this case, find_location( ) will return the name of the city nearest latitude 32.1 and longitude 35.7 (which might be a heavily populated area) whose population rank is between 1 and 100. The statement then returns the zip code that corresponds to that city.
The WHERE clause of the SQL statement must produce an SLV that is used within other parts of the statement. The following SELECT statement is invalid because the projection list of the Projection clause produces the SLV:
-- invalid SELECT statement SELECT title, contains(body, 'dog and cat', rank # INT), rank FROM documents
The data type you use when you declare the SLV in a statement must be the same as the data type of the corresponding OUT or INOUT parameter in the CREATE FUNCTION statement. If you use different but compatible data types, such as INTEGER and FLOAT, the database server automatically performs the cast between the data types.
SLVs share the name space with UDR variables and the column names of the table involved in the SQL statement. Therefore, the database uses the following precedence to resolve ambiguous situations:
Once the user-defined function assigns its OUT parameter to the SLV, you can use this SLV value in other parts of the SQL statement. For more information, see Statement-Local Variable Expressions (IDS).
The Statement-Local Variable Expression specifies a statement-local variable (SLV) that you can use elsewhere in the same SQL statement.
Statement-Local Variable Expressions: |--SLV_variable-------------------------------------------------|
Element | Description | Restrictions | Syntax |
---|---|---|---|
SLV_variable | Statement-local variable (SLV) assigned in a call to a user-defined function in the same SQL statement | The SLV_variable exists only for the life of the statement. Its name must be unique within the statement | Identifier, p. Identifier |
You define an SLV in the call to a user-defined function in the WHERE clause of the SQL statement. This user-defined function must be defined with one or more OUT or INOUT parameters. The call to the user-defined function assigns the value of the OUT or INOUT parameters to the SLVs. For more information, see Statement-Local Variable Declaration (IDS).
Once the user-defined function assigns its OUT or INOUT parameters to the SLVs, you can use these values in other parts of the SQL statement, subject to the following scope-of-reference rules:
In other words, if a query contains a subquery, an SLV that is visible in the query is also visible to all subqueries of that query.
In other words, if a query contains a subquery and the SLV is defined in the subquery, it is not visible to the parent query.
The SLV is not visible to all other queries involved in the UNION.
Within this SELECT portion, all the above scoping rules apply.
The following SELECT statement calls the find_location( ) function in a WHERE clause and defines the rank SLV. Here find_location( ) accepts two values that represent a latitude and a longitude and return the name of the nearest city with an extra value of type INTEGER that represents the population rank of the city.
SELECT zip_code_t FROM address WHERE address.city = find_location(32.1, 35.7, rank # INT) AND rank < 101;
When execution of the find_location() function completes successfully, the function has initialized the rank SLV. The SELECT then uses this rank value in a second WHERE clause condition. In this example, the Statement-Local Variable Expression is the variable rank in the second WHERE clause condition:
rank < 101
The number of OUT and INOUT parameters and SLVs that a UDF can have is not restricted. (Releases of Dynamic Server earlier than Version 9.4 restricted user-defined functions to a single OUT parameter and no INOUT parameters, thereby restricting the number of SLVs to no more than one.)
If the user-defined function that initializes the SLVs is not executed in an iteration of the statement, the SLVs each have a value of NULL. Values of SLVs do not persist across iterations of the statement. At the start of each iteration, the database server sets the SLV values to NULL.
The following partial statement calls two user-defined functions with OUT parameters, whose values are referenced with the SLV names out1 and out2:
SELECT... WHERE func_2(x, out1 # INTEGER) < 100 AND (out1 = 12 OR out1 = 13) AND func_3(a, out2 # FLOAT) = "SAN FRANCISCO" AND out2 = 3.1416;
If a function assigns one or more OUT or INOUT parameter values from another database of the local database server to SLVs, the values must be of built-in data types, or DISTINCT data types whose base types are built-in data types (and that you explicitly cast to built-in data types), or must be UDTs that you explicitly cast to built-in data types. All the UDTs, DISTINCT types, and casts must be defined in all of the participating databases.
For more information on how to write a user-defined function with OUT or INOUT parameters, see IBM Informix User-Defined Routines and Data Types Developer's Guide.
An aggregate expression uses an aggregate function to summarize selected database data. The built-in aggregate functions have the following syntax.
Aggregate Expressions: |--+-+-COUNT(-+-*)--------------------------------+-------------------+-+--| | | '-| Aggregate Scope Qualifiers |--)-' | | | '-+-AVG------+--(--+-------------------------------------------+-' | | +-MAX------+ +-| Aggregate Scope Qualifiers |--)---------+ | | +-MIN------+ | .-ALL-. (1) | | | +-SUM------+ '-+-----+--| Subset of Expression |-------)-' | | +-RANGE----+ | | +-STDEV----+ | | '-VARIANCE-' | | (2) (3) | '--------| User-Defined Aggregates |---------------------------------' Aggregate Scope Qualifiers: .-ALL------. |--+----------+--+----------+--column---------------------------| +-DISTINCT-+ +-table .--+ '-UNIQUE---' +-view.----+ '-synonym.-'
Element | Description | Restrictions | Syntax |
---|---|---|---|
column | Column to which aggregate function is applied | See headings for individual keywords on pages that follow | Identifier, p. Identifier |
synonym, table, view | Synonym, table, or view that contains column | Synonym and the table or view to which it points must exist | Database Object Name, p. Database Object Name |
You cannot use an aggregate expression in a condition that is part of a WHERE clause unless you use the aggregate expression within a subquery. You cannot apply an aggregate function to a BYTE or TEXT column. For other general restrictions, see Subset of Expressions Valid in an Aggregate Expression.
An aggregate function returns one value for a set of queried rows. The following examples show aggregate functions in SELECT statements:
SELECT SUM(total_price) FROM items WHERE order_num = 1013 SELECT COUNT(*) FROM orders WHERE order_num = 1001 SELECT MAX(LENGTH(fname) + LENGTH(lname)) FROM customer
If you use an aggregate function and one or more columns in the projection list of the Projection clause, you must put all the column names that are not used as part of an aggregate or time expression in the GROUP BY clause.
Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]