I have got simple functions to work in DB2 but I am unable to get any functions to work which declare a global temporary table. I am aware the DB2 version and platform is vastly different but I thought this should be possible as it worked for this poster.
CREATE FUNCTION FuncTest1 ()
RETURNS TABLE
(
USE_NAME VARCHAR(48),
USE_PARTNER_LINK FLOAT
)
LANGUAGE SQL
MODIFIES SQL DATA
DETERMINISTIC
BEGIN
DECLARE GLOBAL TEMPORARY TABLE USE_TRUNC
(
USE_NAME VARCHAR(48) NULL,
USE_PARTNER_LINK FLOAT NULL
);
INSERT INTO SESSION.USE_TRUNC
(USE_NAME,USE_PARTNER_LINK)
SELECT USE_NAME,USE_PARTNER_LINK FROM F_USERS;
RETURN
SELECT USE_NAME,USE_PARTNER_LINK FROM F_USERS;
END
The errors vary widely as I try different things but this is the current output:
An unexpected token "USE_NAME" was found following "
RETURN
SELECT". Expected tokens may include: "(".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.9.78
Added fiddle
CREATE FUNCTION (SQL scalar, table, or row) statement, v9.7:
CREATE FUNCTION (SQL scalar, table, or row) statement, v11.1:
You can't use
MODIFIED SQL DATA
withCompound SQL (compiled)
(BEGIN ... END
) for a table function. AndDECLARE GTT
statement is not supported byCompound SQL (inlined)
(BEGIN ATOMIC ... END
).So, rewrite your table function not using Declared GTTs & INSERTs. Or try Created GTT, but create them beforehand (not in the function body).
As for your case
You may use a single
SELECT
statement withCommon Table Expression
: