DB2 LUW 8.1
Colleagues:
It's my understanding that SELECT...INTO is not supported in UDFs. I'm
having problems, however, with (compound) variable assignments
involving CTEs. My highly-contrived example, below, mimics what I want
to. It's important to note that I need to a) assign more than one
column to a variable, and b) use a CTE (as opposed to an in-line
view), because I need to refer to the first CTE in the second CTE.
CREATE FUNCTION TEST() RETURNS DECIMAL
SPECIFIC TEST
DETERMINISTIC
NO EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
BEGIN ATOMIC
DECLARE V_TEST1 INTEGER DEFAULT 0;--
DECLARE V_TEST2 INTEGER DEFAULT 0;--
SET (V_TEST1, V_TEST2) =
(
WITH
T(C1)
AS
(
VALUES
(1)
),
T2(C1)
AS
(
SELECT
*
FROM
T
)
SELECT
COUNT(*),
COUNT(*)
FROM
T2
);--
RETURN CAST(V_TEST1 AS DECIMAL) / V_TEST2;--
END;
The error I'm getting when trying to create the UDF is
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "AS" was found following "H
T(C1) ".
Expected tokens may include: "JOIN". LINE NUMBER=16. SQLSTATE=42601
Is it not possible to use a CTE in a scalar subselect (in a UDF)?
Regards,
--Jeff
Colleagues:
It's my understanding that SELECT...INTO is not supported in UDFs. I'm
having problems, however, with (compound) variable assignments
involving CTEs. My highly-contrived example, below, mimics what I want
to. It's important to note that I need to a) assign more than one
column to a variable, and b) use a CTE (as opposed to an in-line
view), because I need to refer to the first CTE in the second CTE.
CREATE FUNCTION TEST() RETURNS DECIMAL
SPECIFIC TEST
DETERMINISTIC
NO EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
BEGIN ATOMIC
DECLARE V_TEST1 INTEGER DEFAULT 0;--
DECLARE V_TEST2 INTEGER DEFAULT 0;--
SET (V_TEST1, V_TEST2) =
(
WITH
T(C1)
AS
(
VALUES
(1)
),
T2(C1)
AS
(
SELECT
*
FROM
T
)
SELECT
COUNT(*),
COUNT(*)
FROM
T2
);--
RETURN CAST(V_TEST1 AS DECIMAL) / V_TEST2;--
END;
The error I'm getting when trying to create the UDF is
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0104N An unexpected token "AS" was found following "H
T(C1) ".
Expected tokens may include: "JOIN". LINE NUMBER=16. SQLSTATE=42601
Is it not possible to use a CTE in a scalar subselect (in a UDF)?
Regards,
--Jeff
Comment