DB2自定義函式(C語言)
1 總體介紹
基於DB2 V9.5
1.1 使用者自定義函式(UDF)
DB2 內部提供了大量的函式,但仍然不足以滿足業務千奇百怪的需要。不過DB2提供了使用者自定義函式(User Defined Function,簡稱 UDF)功能。DB29.5版本的自定義函式實現,除過DB2自帶的 SQL PL,還支援高階語言,包括:
- ADO.NET
- .NET Common Language Runtime programming languages
- CLI
- Embedded SQL
- C
- C++
- COBOL (Only supported for procedures)
- JDBC
- Java
- Java
- OLE
- Visual Basic
- Visual C++
- Any other programming language that supports this API.
- OLE DB (Only supported for table functions)
- Any programming language that supports this API.
- SQLJ
- Java
1.2 使用者自定義函式的分類
使用者自定義函式分為三種類型:
(1)標量函式(scalar functions)。返回一個單值的函式稱為標量函式。例如,DB2內建了大量標量函式,如abs、length、year、replace
內建標量函式參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/c0000757.html
(2)聚集函式(Aggregate functions)。有人稱為列函式,從表的一列中進行統計返回單值答案。例如,求某個欄位平均值的函式 AVG()。
內建聚集函式參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/c0000767.html
(3
內建表函式參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/c0002372.html
2 C語言自定義函式
2.1 建立過程
1、 編寫c檔案
2、 編譯c檔案:/opt/IBM/db2/V9.5/samples/c/bldrtn 無後綴程式名稱資料庫名稱
3、 執行sql:CREATEFUNCTION語句,建立自定義函式
4、 測試
注:使用例項使用者編譯,然後可賦權給其他使用者呼叫
2.2 重要概念
2.2.1 SQLUDF_SCRAT快取記憶體
一個快取記憶體,一般用在表函式,為多次函式呼叫之間儲存狀態資訊。與create function的SCRATCHPAD引數相對應。
DB2 通過使用所謂的“高速暫存(scratchpad)”提供了在 UDF呼叫之間傳遞資訊的機制。此外,您可以標識特定呼叫“型別”;即它是對該 UDF 的第一次呼叫、普通呼叫還是最後一次(最終)呼叫。使用高速暫存和呼叫型別,有可能只對模式編譯一次,然後將該已編譯模式的內部表示法重用於對該 UDF的所有後續呼叫。在最後一次呼叫時,釋放在處理期間分配的資源。
在標頭檔案sqludf.h中定義如下:
/* Forthe scratchpad. Note: SQLUDF_SCRAT is a pointer. */
#define SQLUDF_SCRAT (sqludf_scratchpad)
/* scratchpad area */
/******************************************************************************
** Structure used for: the scratchpad.
** Note: This structure as provided by DB2 gives the
** overall size/structure of the scratchpad argument.
** Individual UDFs which use a scratchpad may wish to
** override the "data" variable of thisscratchpad
** with variables that make sense for the particular
** UDF.
** Note: Remember that the "data" variableis initialized
** by DB2 to all \0 before the first call.
*******************************************************************************/
SQL_STRUCTURE sqludf_scratchpad
{
sqluint32 length; /* length of scratchpad data */
char data[SQLUDF_SCRATCHPAD_LEN];
/* scratchpad data, init. to */
/* all \0 */
};
參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.apdv.routines.doc/doc/c0023761.html
2.2.2 call-type
主要用在表函式,為表函式呼叫的初始化、開始、下一行、結束、清理等資訊
在標頭檔案sqludf.h中定義如下:
/* Forthe call type. Note: pointer to enum type is dereferenced. */
#define SQLUDF_CALLT (*sqludf_call_type)
/* call type (used with scr'pad) */
#define SQLUDF_CALL_TYPE
enum sqludf_call_type /*for call type */
/******************************************************************************
** Enumerated type, for call-type argument.
*******************************************************************************/
/* Contains generic internal values; right below, wewill map to */
/* these values for scalar and table function calltypes. */
enum sqludf_call_type
{
SQLUDF_ENUM_CALLTYPE_MINUS2 = -2,
/* Used in table func for FIRST call */
SQLUDF_ENUM_CALLTYPE_MINUS1 = -1,
/* Used in table func for OPEN call, */
/* and scalar func FIRST call */
SQLUDF_ENUM_CALLTYPE_ZERO = 0,
/* Used in table func for FETCH call, */
/* and scalar func NORMAL call */
SQLUDF_ENUM_CALLTYPE_PLUS1 = 1,
/* Used in table func for CLOSE call, */
/* and scalar func FINAL call */
SQLUDF_ENUM_CALLTYPE_PLUS2 = 2,
/* Used in table func for FINAL call, */
SQLUDF_ENUM_CALLTYPE_PLUS255 = 255,
/* Used in table func FINAL_CRA call */
/* and scalar func FINAL_CRA call */
SQLUDF_ENUM_CALLTYPE_END = 0x7FFFFFFF
/* Present only to force 4byte type */
};
2.2.3 dbinfo
DB2會傳遞一個數據結構,其中包含的資訊包括當前連線的資料庫的名稱、應用程式執行時授權 ID、呼叫此函式的資料庫伺服器的版本、釋出版本和修訂級別以及伺服器使用的作業系統
The dbinfostructure is a structure that contains database and routine information thatcan be passed to and from a routine implementation as an extra argument if andonly if the DBINFO clause is included in the CREATE statement for the routine.
參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.apdv.routines.doc/doc/c0023760.html
2.3 編碼規範
標頭檔案:
#include <sqludf.h>
函式引數:
按照順序與create function對應,c函式的引數為:inputParam1、inputParam2….,outputParam1、outputParam2….,還需要針對每一個引數提供指示變數,最後為獲取診斷資訊的變數SQLUDF_TRAIL_ARGS或SQLUDF_TRAIL_ARGS_ALL(函式使用快取記憶體SCRATCHPAD時使用)
SQL_API_RC SQL_API_FNfunction-name ( SQL-arguments,
SQL-argument-inds,
SQLUDF_TRAIL_ARGS )
SQL_API_RCSQL_API_FN
SQL_API_RC andSQL_API_FN are macros that specify the return type and calling convention for aC or C++ user-defined function, which can vary across supported operatingsystems. The use of these macros is required for C and C++ routines. The macrosare declared in embedded SQL application and routine include file sqlsystm.h.
function-name
Name of the C orC++ function within the code file. This value does not have to be the same asthe name of the function specified within the corresponding CREATE FUNCTIONstatement. This value in combination with the library name however must bespecified in the EXTERNAL NAME clause to identify the correct function entrypoint within the library to be used. For C++ routines, the C++ compiler appliestype decoration to the entry point name. Either the type decorated name needsto be specified in the EXTERNAL NAME clause, or the function declaration withinthe source code file should be prefixed with extern "C" as shown inthe following example: extern "C" SQL_API_RC SQL_API_FN OutLanguage(char *, sqlint16 *, char *, char *, char *, char *);
SQL-arguments
C or C++ argumentsthat correspond to the set of SQL parameters specified in the CREATE FUNCTIONstatement.
SQL-argument-inds
For eachSQL-argument a null indicator parameter is required to specify whether theparameter value is intended to be interpreted within the routine implementationas a NULL value in SQL. Null indicators must be specified with data typeSQLUDF_NULLIND. This data type is defined in embedded SQL routine include filesqludf.h.
SQLUDF_TRAIL_ARGS
A macro defined inembedded SQL routine include file sqludf.h that once expanded defines theadditional trailing arguments required for a complete parameter style SQLsignature. There are two macros that can be used: SQLUDF_TRAIL_ARGS andSQLUDF_TRAIL_ARGS_ALL. SQLUDF_TRAIL_ARGS when expanded, as defined in sqludf.h,is equivalent to the addition of the following routine arguments:
SQLUDF_CHAR*sqlState,
SQLUDF_CHARqualName,
SQLUDF_CHARspecName,
SQLUDF_CHAR*sqlMessageText,
In general thesearguments are not required or generally used as part of user-defined functionlogic. They represent the output SQLSTATE value to be passed back to thefunction invoker, the input fully qualified function name, input functionspecific name, and output message text to be returned with the SQLSTATE.SQLUDF_TRAIL_ARGS_ALL when expanded, as defined in sqludf.h, is equivalent tothe addition of the following routine arguments:
SQLUDF_CHAR qualName,
SQLUDF_CHAR specName,
SQLUDF_CHAR sqlMessageText,
SQLUDF_SCRAT*scratchpad
SQLUDF_CALLT*callType
If the UDF CREATEstatement includes the SCRATCHPAD clause or the FINAL CALL clause, then themacro SQLUDF_TRAIL_ARGS_ALL must be used. In addition to arguments providedwith SQLUDF_TRAIL_ARGS, this macro also contains pointers to a scratchpadstructure, and a call type value.
參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.apdv.routines.doc/doc/c0023763.html
SQL與c引數對應列表:
SQL Column Type |
C/C++ Data Type |
SQL Column Type Description |
SMALLINT |
sqlint16 |
16-bit signed integer |
INTEGER |
sqlint32 |
32-bit signed integer |
BIGINT |
sqlint64 |
64-bit signed integer |
REAL |
float |
Single-precision floating point |
DOUBLE |
double |
Double-precision floating point |
DECIMAL(p, s) |
Not supported. |
To pass a decimal value, define the parameter to be of a data type castable from DECIMAL (for example CHAR or DOUBLE) and explicitly cast the argument to this type. |
CHAR(n) |
char[n+1] where n is large enough to hold the data 1<=n<=254 |
Fixed-length, null-terminated character string |
CHAR(n) FOR BIT DATA |
char[n] where n is large enough to hold the data 1<=n<=254 |
Fixed-length, not null-terminated character string |
VARCHAR(n) |
char[n+1] where n is large enough to hold the data 1<=n<=32 672 |
Null-terminated varying length string |
VARCHAR(n) FOR BIT DATA |
struct { |
Not null-terminated varying length character string |
LONG VARCHAR |
struct { |
Not null-terminated varying length character string |
CLOB(n) |
struct { |
Not null-terminated varying length character string with 4-byte string length indicator |
BLOB(n) |
struct { |
Not null-terminated varying binary string with 4-byte string length indicator |
DATE |
char[11] |
Null-terminated character string of the following format: yyyy-mm-dd |
TIME |
char[9] |
Null-terminated character string of the following format: hh.mm.ss |
TIMESTAMP |
char[27] |
Null-terminated character string of the following format: yyyy-mm-dd-hh.mm.ss.nnnnnn |
LOB LOCATOR |
sqluint32 |
32-bit signed integer |
GRAPHIC(n) |
sqldbchar[n+1] where n is large enough to hold the data 1<=n<=127 |
Fixed-length, null-terminated double-byte character string |
VARGRAPHIC(n) |
sqldbchar[n+1] where n is large enough to hold the data 1<=n<=16 336 |
Null-terminated, variable-length double-byte character string |
LONG VARGRAPHIC |
struct { |
Not null-terminated, variable-length double-byte character string |
DBCLOB(n) |
struct { |
Not null-terminated varying length character string with 4-byte string length indicator |
XML AS CLOB |
struct { |
Not null-terminated varying length serialized character string with 4-byte string length indicator. |
參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.apdv.routines.doc/doc/r0009730.html
快取記憶體:
struct scratchMap {
char *p;
int rowNumber;
};
/* map the scratchpad */
struct scratchMap *scratch = (struct scratchMap *)SQLUDF_SCRAT->data;
參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.apdv.routines.doc/doc/c0023761.html
其他:
Ø 支援採用嵌入式sql或呼叫cli方式呼叫執行sql語句
Ø 不支援printf輸出資訊,建議寫入一個檔案、或者寫入SQLUDF_TRAIL_ARG中的sqlMessageText欄位
Ø 診斷資訊長度有限
2.4 CREATE FUNCTION語句
2.4.1 CREATE FUNCTION (標量函式) 子句
>>-CREATE FUNCTION--function-name------------------------------->
>--(--+-------------------------------+--)--●------------------->
| .-,-------------------------. |
| V | |
'---| parameter-declaration |-+-'
>--RETURNS--+-| data-type2 |--+------------+----------------------------+-->
| '-AS LOCATOR-' |
'-| data-type3 |--CAST FROM--| data-type4 |--+------------+-'
'-AS LOCATOR-'
>--●--+-------------------------+--●---------------------------->
'-SPECIFIC--specific-name-'
>--EXTERNAL--+----------------------+--●------------------------>
'-NAME--+-'string'---+-'
'-identifier-'
(1)
>--LANGUAGE--+-C----+------●--PARAMETER STYLE--+-DB2GENERAL-+--->
+-JAVA-+ +-JAVA-------+
+-CLR--+ '-SQL--------'
'-OLE--'
>--●--+------------------------------+--●----------------------->
'-PARAMETER CCSID--+-ASCII---+-'
'-UNICODE-'
.-NOT DETERMINISTIC-.
>--+-------------------+--●------------------------------------->
'-DETERMINISTIC-----'
.-FENCED------------------------.
>--+-------------------------------+--●------------------------->
+-FENCED--●--+-THREADSAFE-----+-+
| '-NOT THREADSAFE-' |
| .-THREADSAFE-. |
'-NOT FENCED--●--+------------+-'
.-READS SQL DATA-.
>--+----------------------------+--●--+----------------+--●----->
+-RETURNS NULL ON NULL INPUT-+ +-NO SQL---------+
'-CALLED ON NULL INPUT-------' '-CONTAINS SQL---'
.-STATIC DISPATCH-. .-EXTERNAL ACTION----.
>--+-----------------+--●--+--------------------+--●------------>
'-NO EXTERNAL ACTION-'
.-NO SCRATCHPAD----------. .-NO FINAL CALL-.
>--+------------------------+--●--+---------------+--●---------->
| .-100----. | '-FINAL CALL----'
'-SCRATCHPAD--+--------+-'
'-length-'
.-NO DBINFO-.
>--+-------------------+--●--+-----------+--●------------------->
+-ALLOW PARALLEL----+ '-DBINFO----'
'-DISALLOW PARALLEL-'
>--+-----------------------------+--●--------------------------->
'-TRANSFORM GROUP--group-name-'
>--+-----------------------------------------------+--●--------->
'-PREDICATES--(--| predicate-specification |--)-'
.-INHERIT SPECIAL REGISTERS-.
>--+---------------------------+--●----------------------------><
parameter-declaration
|--+----------------+--| data-type1 |--+------------+-----------|
'-parameter-name-' '-AS LOCATOR-'
data-type1, data-type2, data-type3, data-type4
|--+-| built-in-type |----+-------------------------------------|
+-distinct-type-name---+
+-structured-type-name-+
'-REF--(--type-name--)-'
built-in-type
|--+-+-SMALLINT----+-------------------------------------------------------+--|
| +-+-INTEGER-+-+ |
| | '-INT-----' | |
| '-BIGINT------' |
| .-(5,0)-------------------. |
+-+-+-DECIMAL-+-+--+-------------------------+--------------------------+
| | '-DEC-----' | | .-,0-------. | |
| '-+-NUMERIC-+-' '-(integer-+----------+-)-' |
| '-NUM-----' '-,integer-' |
| .-(53)------. |
+-+-FLOAT--+-----------+--+---------------------------------------------+
| | '-(integer)-' | |
| +-REAL------------------+ |
| | .-PRECISION-. | |
| '-DOUBLE--+-----------+-' |
| .-(1)-------. |
+-+-+-+-CHARACTER-+--+-----------+----------+--+--------------------+-+-+
| | | '-CHAR------' '-(integer)-' | | (2) | | |
| | '-+-VARCHAR----------------+--(integer)-' '-------FOR BIT DATA-' | |
| | '-+-CHARACTER-+--VARYING-' | |
| | '-CHAR------' | |
| | .-(1M)-------------. | |
| '-+-CLOB------------------------+--+------------------+-------------' |
| '-+-CHARACTER-+--LARGE OBJECT-' '-(integer-+---+-)-' |
| '-CHAR------' +-K-+ |
| +-M-+ |
| '-G-' |
| .-(1)-------. |
+-+-GRAPHIC--+-----------+-------+--------------------------------------+
| | '-(integer)-' | |
| +-VARGRAPHIC--(integer)--------+ |
| | .-(1M)-------------. | |
| '-DBCLOB--+------------------+-' |
| '-(integer-+---+-)-' |
| +-K-+ |
| +-M-+ |
| '-G-' |
| .-(1M)-------------. |
+-+-BLOB----------------+--+------------------+-------------------------+
| '-BINARY LARGE OBJECT-' '-(integer-+---+-)-' |
| +-K-+ |
| +-M-+ |
| '-G-' |
+-+-DATE------+---------------------------------------------------------+
| +-TIME------+ |
| '-TIMESTAMP-' |
+-XML-------------------------------------------------------------------+
| .-SYSPROC.-. (3) (4) |
'-+----------+--DB2SECURITYLABEL----------------------------------------'
predicate-specification
|--WHEN--+- = -+--+-constant-----------------------+----------->
+- <> -+ '-EXPRESSION AS--expression-name-'
+- < -+
+- > -+
+- <= -+
'- >= -'
>--+-| data-filter |--+------------------------+-+--------------|
| '-| index-exploitation |-' |
'-| index-exploitation |--+-----------------+-'
'-| data-filter |-'
data-filter
|--FILTER USING--+-function-invocation-+------------------------|
'-case-expression-----'
index-exploitation
|--SEARCH BY--+-------+--INDEX EXTENSION--index-extension-name-->
'-EXACT-'
.-----------------------.
V |
>----| exploitation-rule |-+------------------------------------|
exploitation-rule
|--WHEN KEY--(--parameter-name1--)------------------------------>
.-,---------------.
V |
>--USE--search-method-name--(----parameter-name2-+--)-----------|
參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0004461.html
2.4.2 CREATE FUNCTION (表函式) 子句
>>-CREATE FUNCTION--function-name------------------------------->
>--(--+-------------------------------+--)--●------------------->
| .-,-------------------------. |
| V ||
'---| parameter-declaration |-+-'
.-,-------------------------------------------.
V |
>--RETURNS TABLE--(----column-name--|data-type2 |--+------------+-+--)-->
'-ASLOCATOR-'
>--●--+-------------------------+--●---------------------------->
'-SPECIFIC--specific-name-'
>--EXTERNAL--+----------------------+--●------------------------>
'-NAME--+-'string'---+-'
'-identifier-'
(1)
>--LANGUAGE--+-C----+------●--PARAMETERSTYLE--+-DB2GENERAL-+--->
+-JAVA-+ '-SQL--------'
+-CLR--+
'-OLE--'
>--●--+------------------------------+--●----------------------->
'-PARAMETER CCSID--+-ASCII---+-'
'-UNICODE-'
.-NOT DETERMINISTIC-.
>--+-------------------+--●------------------------------------->
'-DETERMINISTIC-----'
.-FENCED------------------------.
>--+-------------------------------+--●------------------------->
+-FENCED--●--+-THREADSAFE-----+-+
| '-NOT THREADSAFE-'|
| .-THREADSAFE-.|
'-NOT FENCED--●--+------------+-'
.-RETURNS NULL ON NULL INPUT-. .-READS SQL DATA-.
>--+----------------------------+--●--+----------------+--●----->
'-CALLED ON NULL INPUT-------' +-NO SQL---------+
'-CONTAINS SQL---'
.-STATIC DISPATCH-. .-EXTERNALACTION----.
>--+-----------------+--●--+--------------------+--●------------>
'-NO EXTERNALACTION-'
.-NO SCRATCHPAD----------. .-NO FINAL CALL-.
>--+------------------------+--●--+---------------+--●---------->
| .-100----. | '-FINAL CALL----'
'-SCRATCHPAD--+--------+-'
'-length-'
>--+-DISALLOWPARALLEL------------------------------------------------------------------+-->
| .-DATABASE PARTITIONS-. |
'-ALLOW PARALLEL--EXECUTE ON--ALL--+---------------------+--RESULT TABLEDISTRIBUTED-'
.-NO DBINFO-.
>--●--+-----------+--●--+----------------------+--●------------->
'-DBINFO----' '-CARDINALITY--integer-'
>--+-----------------------------+--●--------------------------->
'-TRANSFORM GROUP--group-name-'
.-INHERIT SPECIAL REGISTERS-.
>--+---------------------------+--●----------------------------><
parameter-declaration
|--+----------------+--| data-type1|--+------------+-----------|
'-parameter-name-' '-AS LOCATOR-'
data-type1, data-type2
|--+-| built-in-type|----+-------------------------------------|
+-distinct-type-name---+
+-structured-type-name-+
'-REF--(--type-name--)-'
built-in-type
|--+-+-SMALLINT----+-------------------------------------------------------+--|
|+-+-INTEGER-+-+ |
|| '-INT-----' | |
|'-BIGINT------' |
| .-(5,0)-------------------. |
+-+-+-DECIMAL-+-+--+-------------------------+--------------------------+
|| '-DEC-----' | | .-,0-------. | |
|'-+-NUMERIC-+-' '-(integer-+----------+-)-' |
| '-NUM-----' '-,integer-' |
| .-(53)------. |
+-+-FLOAT--+-----------+--+---------------------------------------------+
|| '-(integer)-' | |
|+-REAL------------------+ |
|| .-PRECISION-. | |
|'-DOUBLE--+-----------+-' |
| .-(34)-. |
+-DECFLOAT--+------+----------------------------------------------------+
| '-(16)-' |
| .-(1)-------. |
+-+-+-+-CHARACTER-+--+-----------+----------+--+--------------------+-+-+
|| | '-CHAR------' '-(integer)-' | | (2) | | |
|| '-+-VARCHAR----------------+--(integer)-' '-------FOR BIT DATA-' | |
|| '-+-CHARACTER-+--VARYING-' | |
|| '-CHAR------' | |
|| .-(1M)-------------. ||
|'-+-CLOB------------------------+--+------------------+-------------' |
| '-+-CHARACTER-+--LARGEOBJECT-' '-(integer-+---+-)-' |
| '-CHAR------' +-K-+ |
| +-M-+ |
| '-G-' |
| .-(1)-------. |
+-+-GRAPHIC--+-----------+-------+--------------------------------------+
|| '-(integer)-' | |
|+-VARGRAPHIC--(integer)--------+ |
|| .-(1M)-------------. | |
|'-DBCLOB--+------------------+-' |
| '-(integer-+---+-)-' |
| +-K-+ |
| +-M-+ |
| '-G-' |
| .-(1M)-------------. |
+-+-BLOB----------------+--+------------------+-------------------------+
|'-BINARY LARGE OBJECT-' '-(integer-+---+-)-' |
| +-K-+ |
| +-M-+ |
| '-G-' |
+-+-DATE------+---------------------------------------------------------+
|+-TIME------+ |
|'-TIMESTAMP-' |
+-XML-------------------------------------------------------------------+
|.-SYSPROC.-. (3)(4) |
'-+----------+--DB2SECURITYLABEL----------------------------------------'
參考:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0004462.html
2.4.3 引數說明
Ø DETERMINISTIC or NOT DETERMINISTIC:指定是否每當以相同的一組輸入引數執行函式時,都返回相同的結果。確定性(Deterministic)函式包括數學函式和不依賴於表中資料或變化資料來源的函式。
Ø NO SQL, CONTAINS SQL, READS SQL DATA: 指定函式是否執行SQL互動,如何互動。
n NO SQL:外部 UDF 體不包含任何 SQL,或者包含不可執行的SQL語句。(不可執行的SQL語句包括INCLUDE和WHENEVER語句等)。
n CONTAINS SQL:UDF 體包含的可執行SQL語句既不讀資料,也不修改資料。
n READS SQL DATA:UDF 體包含的可執行SQL語句讀資料,但是不修改資料。
Ø FENCED or NOT FENCED:是否在DB2的程序/記憶體空間中執行
Ø RETURNS NULL ON NULL INPUT or CALLED ON NULL INPUT:當輸入資料為NULL時,函式是否直接返回NULL
Ø EXTERNAL ACTION or NO EXTERNAL ACTION:是否應該除過資料庫之外的其他內容,必須在作業系統層面寫檔案等
Ø NO SCRATCHPAD or SCRATCHPAD length:是否指定是否為UDF分配作為持久儲存區域的記憶體,預設length為100。如果指定了SCRATCHPAD子句,DB2就會在首次呼叫此函式時分配適當數量的記憶體。在建立和填充暫存區域之後,會在各次函式呼叫之間保留它的內容—— UDF在一次呼叫中對暫存區域所做的任何修改都會保留到下一次呼叫。
Ø CARDINALITY integer:可選引數,指定表函式返回的行數
Ø NO DBINFO or DBINFO:表示在呼叫函式時是否把 DB2 掌握的資訊作為額外引數傳遞給 UDF(DBINFO表示傳遞,NO DBINFO表示不傳遞)。如果指定了 DBINFO子句,那麼DB2會傳遞一個數據結構,其中包含的資訊包括當前連線的資料庫的名稱、應用程式執行時授權 ID、呼叫此函式的資料庫伺服器的版本、釋出版本和修訂級別以及伺服器使用的作業系統。
2.4.4 示例
--drop function gprsSplit;
CREATE FUNCTION gprsSplit(recordextensionVARCHAR(1025))RETURNS TABLE (idint,busi_typeVARCHAR(10),durationbigint,up_flowbigint,down_flowbigint,feebigint)SPECIFIC gprsSplitEXTERNAL NAME 'gprsSplit_udf!gprsSplit'LANGUAGE C
PARAMETER STYLE SQL
DETERMINISTIC
--NOT DETERMINISTIC --FENCEDNOT FENCED
RETURNS NULL ON NULL INPUT
NO SQL
NO EXTERNAL ACTION
SCRATCHPAD
NO FINAL CALL
DISALLOW PARALLEL
--ALLOW PARALLEL EXECUTE ON ALL DATABASE PARTITIONS RESULT TABLEDISTRIBUTEDCARDINALITY 2;
2.5 注意事項
當您開發新的使用者自定義函式或修改現有的使用者自定義函式時,應該總是通過在資料庫中將它註冊為 FENCED