This statement is used to register a user-defined OLE DB external table function to access data from an OLE DB provider.
A table function may be used in the FROM clause of a SELECT.
Invocation
This statement can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared. However, if the bind option DYNAMICRULES BIND applies, the statement cannot be dynamically prepared (SQLSTATE 42509).
Authorization
The privileges held by the authorization ID of the statement must include at least one of the following:
If the authorization ID has insufficient authority to perform the operation, an error (SQLSTATE 42502) is raised.
Syntax
>>-CREATE FUNCTION--function-name-------------------------------> >----(--+-----------------------------------+---)---*-----------> '-+-----------------+--data-type1---' '-parameter-name--' .-,--------------------------. V | >----RETURNS TABLE--(-----column-name--data-type2---+---)---*---> >-----+--------------------------+--*---------------------------> '-SPECIFIC--specific-name--' >----EXTERNAL--NAME--'string'--*---LANGUAGE----OLEDB-----*------> .-NOT DETERMINISTIC--. >-----+--------------------+--*---------------------------------> | (1) | '-DETERMINISTIC------' .-RETURNS NULL ON NULL INPUT--. (2) >-----+-----------------------------+---------*-----------------> '-CALLED ON NULL INPUT--------' .-NO EXTERNAL ACTION--. >-----+---------------------+--*----+-----------------------+---> '-EXTERNAL ACTION-----' '-CARDINALITY--integer--' >----*---------------------------------------------------------><
Notes:
Description
The name, including the implicit or explicit qualifiers, together with the number of parameters and the data type of each parameter (without regard for any length, precision or scale attributes of the data type) must not identify a function described in the catalog (SQLSTATE 42723). The unqualified name, together with the number and data types of the parameters, while of course unique within its schema, need not be unique across schemas.
If a two-part name is specified, the schema-name cannot begin with "SYS" (SQLSTATE 42939).
A number of names used as keywords in predicates are reserved for system use, and may not be used as a function-name (SQLSTATE 42939). The names are SOME, ANY, ALL, NOT, AND, OR, BETWEEN, NULL, LIKE, EXISTS, IN, UNIQUE, OVERLAPS, SIMILAR, MATCH and the comparison operators as described in Basic Predicate.
The same name can be used for more than one function if there is some difference in the signature of the functions. Although there is no prohibition against it, an external user-defined table function should not be given the same name as a built-in function.
It is possible to register a function that has no parameters. In this case, the parentheses must still be coded, with no intervening data types. For example,
CREATE FUNCTION WOOFER() ...
No two identically-named functions within a schema are permitted to have exactly the same type for all corresponding parameters. Length is not considered in this type comparison. Therefore CHAR(8) and CHAR(35) are considered to be the same type. A duplicate signature raises an SQL error (SQLSTATE 42723).
The specific-name may be the same as an existing function-name.
If no qualifier is specified, the qualifier that was used for function-name is used. If a qualifier is specified, it must be the same as the explicit or implicit qualifier of function-name or an error (SQLSTATE 42882) is raised.
If specific-name is not specified, a unique name is generated by the database manager. The unique name is SQL followed by a character timestamp, SQLyymmddhhmmssxxx.
The 'string' option is a string constant with a maximum of 254 characters.
The string specified is used to establish a connection and session with a OLE DB provider, and retrieve data from a rowset. The OLE DB provider and data source do not need to exist when the CREATE FUNCTION statement is performed. See OLE DB Table Functions in Application Development Guide for more details.
>>-'--+-server--!--+--------+------------------------------------------------+---'--> | '-rowset-' | '-!--+--------+---!--connectstring--+--------------------------------+-' '-rowset-' '-!--COLLATING_SEQUENCE =--+-N-+-' '-Y-' >--------------------------------------------------------------><
Y = Same collating sequence
N = Different collating sequence
If COLLATING_SEQUENCE is not specified, then the data source is assumed to have a different collating sequence from DB2 Universal Database.
If server is provided, connectstring or COLLATING_SEQUENCE are not allowed in the external name. They are defined as server options CONNECTSTRING and COLLATING_SEQUENCE. If no server is provided, a connectstring must be provided. If rowset is not provided, the table function must have an input parameter to pass through command text to the OLE DB provider.
LANGUAGE OLEDB table functions can be created on any platform, but only executed on platforms supported by Microsoft OLE DB.
If RETURNS NULL ON NULL INPUT is specified and if at execution time any one of the function's arguments is null, the user-defined function is not called and the result is the empty table, i.e. a table with no rows.
If CALLED ON NULL INPUT is specified, then at execution time regardless of whether any arguments are null, the user-defined function is called. It can return an empty table or not, depending on its logic. But responsibility for testing for null argument values lies with the UDF.
The value NULL CALL may be used as a synonym for CALLED ON NULL INPUT for backwards and family compatibility. Similarly, NOT NULL CALL may be used as a synonym for RETURNS NULL ON NULL INPUT.
If the CARDINALITY clause is not specified for a table function, DB2 will assume a finite value as a default- the same value assumed for tables for which the RUNSTATS utility has not gathered statistics.
Warning: if a function does in fact have infinite cardinality, i.e. it returns a row every time it is called to do so, never returning the "end-of-table" condition, then queries which require the "end-of-table" condition to correctly function will be infinite, and will have to be interrupted. Examples of such queries are those involving GROUP BY and ORDER BY. The user is advised to not write such UDFs.
Notes
Examples
Example 1: The following registers an OLE DB table function, which retrieves order information from a Microsoft Access database. The connection string is defined in the external name.
CREATE FUNCTION orders () RETURNS TABLE (orderid INTEGER, customerid CHAR(5), employeeid INTEGER, orderdate TIMESTAMP, requireddate TIMESTAMP, shippeddate TIMESTAMP, shipvia INTEGER, freight dec(19,4)) LANGUAGE OLEDB EXTERNAL NAME '!orders!Provider=Microsoft.Jet.OLEDB.3.51; Data Source=c:\sqllib\samples\oledb\nwind.mdb !COLLATING_SEQUENCE=Y';
Example 2: The following registers an OLE DB table function, which retrieves customer information from an Oracle database. The connection string is provided through a server definition. The table name is fully qualified in the external name. The local user john is mapped to the remote user dave. Other users will use the guest userid in the connection string. Refer to CREATE SERVER, CREATE WRAPPER and CREATE USER MAPPING for details on the statements.
CREATE SERVER spirit WRAPPER OLEDB OPTIONS (CONNECTSTRING 'Provider=MSDAORA;Persist Security Info=False; User ID=guest;password=pwd;Locale Identifier=1033; OLE DB Services=CLIENTCURSOR;Data Source=spirit'); CREATE USER MAPPING FOR john SERVER spirit OPTIONS (REMOTE_AUTHID 'dave', REMOTE_PASSWORD 'mypwd'); CREATE FUNCTION customers () RETURNS TABLE (customer_id INTEGER, name VARCHAR(20), address VARCHAR(20), city VARCHAR(20), state VARCHAR(5), zip_code INTEGER) LANGUAGE OLEDB EXTERNAL NAME 'spirit!demo.customer';
Example 3: The following registers an OLE DB table function, which retrieves information about stores from a MS SQL Server 7.0 database. The connection string is provided in the external name. The table function has an input parameter to pass through command text to the OLE DB provider. The rowset name does not need to be specified in the external name. The query example passes in a SQL command text to retrieve the top 3 stores.
CREATE FUNCTION favorites (varchar(600)) RETURNS TABLE (store_id CHAR (4), name VARCHAR (41), sales INTEGER) SPECIFIC favorites LANGUAGE OLEDB EXTERNAL NAME '!!Provider=SQLOLEDB.1;Persist Security Info=False; User ID=sa;Initial Catalog=pubs;Data Source=WALTZ; Locale Identifier=1033;Use Procedure for Prepare=1; Auto Translate=False;Packet Size=4096;Workstation ID=WALTZ; OLE DB Services=CLIENTCURSOR;'; SELECT * FROM TABLE (favorites (' select top 3 sales.stor_id as store_id, ' || ' stores.stor_name as name, ' || ' sum(sales. qty) as sales ' || ' from sales, stores ' || ' where sales.stor_id = stores.stor_id ' || ' group by sales.stor_id, stores.stor_name' || ' order by sum(sales.qty) desc')) as f;