Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / April 2006

Tip: Looking for answers? Try searching our database.

Creating a simple UDF

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amit - 18 Apr 2006 16:49 GMT
I'm trying to create a simple UDF like so:

CREATE FUNCTION MYSCHEMA.GETUSERDEPT(USR_ID  INTEGER)
    RETURNS VARCHAR ( 256 )
    LANGUAGE SQL
    NO EXTERNAL ACTION
RETURN SELECT DEPT_NAME FROM ABC.DEPT WHERE DEPT.USR_ID = USR_ID;

Simple enough, right? When I try to run it, I get this error:

SQL0199N  The use of the reserved word "FROM" following "" is not
valid.
Expected tokens may include:  "ON AFTER <INTEGER>".  SQLSTATE=42601

I know I'm missing something minor but can't figure it out

I'm using DB2 OS/390 8.1.0.

Thanks
Amit
Serge Rielau - 18 Apr 2006 18:55 GMT
> I'm trying to create a simple UDF like so:
>
[quoted text clipped - 3 lines]
>     NO EXTERNAL ACTION
> RETURN SELECT DEPT_NAME FROM ABC.DEPT WHERE DEPT.USR_ID = USR_ID;
You return a scalar subquery. Try wrapping it into braces:
RETURN (SELECT DEPT_NAME FROM ABC.DEPT WHERE DEPT.USR_ID = USR_ID);

DB2 for LUW is more relaxed here because it supports table functions.
DB2 zOS does not so:
RETURN <expr>

<expr> = [.... | ( <subselect> )]

Cheers
Serge

Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Amit - 18 Apr 2006 20:44 GMT
Thanks for your reply, I made some progress. I tried putting
parenthesis around the SELECT statement but now I get this error:

[IBM][DB2] SQL0214N  An expression in the ORDER BY clause in the
following position, or starting with "0" in the "RETURN" clause is not
valid.  Reason code = "7".  SQLSTATE=42822

This is the exact sql:

CREATE FUNCTION MYSCHEMA.GETUSERDEPT(
        USR_ID        INTEGER)
    RETURNS VARCHAR ( 256 )
    LANGUAGE SQL
    NO EXTERNAL ACTION
RETURN (SELECT DEPT_NM FROM ABC.DEPT WHERE DEPT.USR_ID = USR_ID);

-Amit
Serge Rielau - 18 Apr 2006 21:24 GMT
Well, we got past the syntax error.. That's good.
Staring at the zOS docs I see:

The expression cannot contain a scalar fullselect.

Bummer...
Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Amit - 18 Apr 2006 21:32 GMT
D'oh! That sucks.
Thanks anyways!

-Amit
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.