473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CTEs in UDFs

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

Jun 27 '08 #1
4 2378
You can't use CTE in SET statement even in SQL Stored Procedure.

You wouldn't be neccesary to use SET variables, if you used CTE and
the SET statement was not in a loop.
For example:
CREATE FUNCTION TEST() RETURNS DECIMAL
SPECIFIC TEST
DETERMINISTIC
NO EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
RETURN
WITH
T(C1)
AS
(
VALUES
(1)
),
T2(C1)
AS
(
SELECT
*
FROM
T
),
SET1(V_TEST1, V_TEST2) AS (
SELECT
COUNT(*),
COUNT(*)
FROM
T2
)
SELECT
CAST(V_TEST1 AS DECIMAL) / V_TEST2
FROM SET1
;
Jun 27 '08 #2
On Jun 17, 2:40 am, Tonkuma <tonk...@fiberbit.netwrote:
You can't use CTE in SET statement even in SQL Stored Procedure.

You wouldn't be neccesary to use SET variables, if you used CTE and
the SET statement was not in a loop.
For example:
CREATE FUNCTION TEST() RETURNS DECIMAL
SPECIFIC TEST
DETERMINISTIC
NO EXTERNAL ACTION
INHERIT SPECIAL REGISTERS
RETURN
WITH
T(C1)
AS
(
VALUES
(1)
),
T2(C1)
AS
(
SELECT
*
FROM
T
),
SET1(V_TEST1, V_TEST2) AS (
SELECT
COUNT(*),
COUNT(*)
FROM
T2
)
SELECT
CAST(V_TEST1 AS DECIMAL) / V_TEST2
FROM SET1
;
Thanks, Tonkuma. I ended up going with a "RETURNS TABLE" UDF, which
your reply confirmed was the way I had to go....

Regards,

--Jeff
Jun 27 '08 #3
jefftyzzer wrote:
Thanks, Tonkuma. I ended up going with a "RETURNS TABLE" UDF, which
your reply confirmed was the way I had to go....
There is no need to go to a table function as Tonkuma also showed.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Jun 27 '08 #4
On Jun 19, 7:25 am, Serge Rielau <srie...@ca.ibm.comwrote:
jefftyzzer wrote:
Thanks, Tonkuma. I ended up going with a "RETURNS TABLE" UDF, which
your reply confirmed was the way I had to go....

There is no need to go to a table function as Tonkuma also showed.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
I see. I thought that even a 1-column, 1-row result was still a result
*set* (as it came from a SELECT) and the UDF would thus need to be
declared as returning a table.

--Jeff
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew Mayo | last post by:
There is something very strange going on here. Tested with ADO 2.7 and MSDE/2000. At first, things look quite sensible. You have a simple SQL query, let's say select * from mytab where col1 =...
1
by: Eugene | last post by:
In a DB2 V8.1 FP4 database I am trying to create a table SQL UDF that is to return a contents of a temporary table with in this UDF: create function getitemdata(pint int) returns table (...
3
by: David Carver | last post by:
We are running into a problem with a Communication Link failure when calling an External Stored procedure written in ILE Cobol from an SQL UDF. When calling the stored procedure by itself and not...
0
by: Fan Ruo Xin | last post by:
The developer rewrote one C program. So I need to replace the current library, drop and recreate the UDFs. I don't want to restart the db server, I just terminate all the sessions which might call...
4
by: Pete H | last post by:
Hi All; I'm trying to get some of the samples that are amply illustrated in multiple docs to work. When I try to create a Warehouse Center view "...for MQ Series messages" or use the UDF wizard in...
5
by: Gustavo Randich | last post by:
Hello, (Using DB2/LINUX 8.2.0) HOW-TO: recreate a big set of interdependent UDFs... without having to do it manually because of the imposed drop order due to the RESTRICT keyword of the DROP...
7
by: Rhino | last post by:
I am updating some Java UDFs from DB2GENERAL to DB2JAVA as suggested in the manuals for DB2 Version 8 but I'm having problems with setSQLstate() and setSQLmessage(). If I'm reading the manuals...
0
by: Helmut Tessarek | last post by:
Hi everybody, I've written some UDFs to generate passwords within DB2. They are compatible to the functions that are used in Apache's htpasswd utility. Maybe someone can use them. ...
6
by: Carsten | last post by:
Hello Folks, I encountered a problem with SQL server 2000 and UDFs. I have a scalar UDF and a table UDF where I would like the scalar UDF to provide the argument for the table UDF like in: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.