DB2基于JAVA的自定义函数(user-defined-function)示例, 作用是返回两个数的和。
$ db2level
DB21085I This instance or install (instance name, where applicable: "e105q5a")
uses "64" bits and DB2 code release "SQL10055" with level identifier
"0606010E".
Informational tokens are "DB2 v10.5.0.5", "s141128", "IP23626", and Fix Pack
"5".
Product is installed at "/opt/IBM/db2/V10.5.5".
$ oslevel -s
6100-08-02-1316
import COM.ibm.db2.app.*;
import java.sql.*;
public class MyCalculate extends UDF{
public static int mySum(int a, int b) throws SQLException {
return a + b;
}
}
$ /home/db2users/e105q5a/sqllib/java/jdk64/bin/javac MyCalculate.java
$ cp MyCalculate.class $HOME/sqllib/function
$ cat crtFn.txt
CREATE FUNCTION mqssum( numbera int, numberb int)
RETURNS INT
LANGUAGE java
PARAMETER STYLE java
NO SQL
FENCED THREADSAFE
DETERMINISTIC
RETURNS NULL ON NULL INPUT
NO EXTERNAL ACTION
EXTERNAL NAME 'MyCalculate!mySum'
@
$ db2 -td@ -f crtFn.txt
DB20000I The SQL command completed successfully.
$ db2 "select mqssum(3,4) from sysibm.sysdummy1"
1
-----------
7
1 record(s) selected.