DB2 的自定义函数,默认不允许修改表数据,有INSERT UPDATE DELETE之类的语句则会报错:
The "MODIFIES SQL DATA" clause has not been specified in the CREATE FUNCTION statement for LANGUAGE SQL function "UMP.SEQ" but an examination of the function body reveals that it should be specified.. SQLCODE=-374, SQLSTATE=428C2, DRIVER=3.63.108
如果需要修改表数据,则需要增加 MODIFIES SQL DATA 的修饰符,并且返回类型必须是TABLE,否则会报错:
An SQL TABLE function must return a table result.. SQLCODE=-20120, SQLSTATE=428F1, DRIVER=3.63.108
完整示例:
CREATE FUNCTION seq(seq_name char (20)) RETURNS TABLE(col bigint) MODIFIES SQL DATA
BEGIN ATOMIC
UPDATE seq SET val=val+1 WHERE NAME=seq_name;
RETURN (SELECT val FROM seq WHERE NAME=seq_name);
END