473,386 Members | 1,864 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,386 software developers and data experts.

Passing a parameter to IN predicate of DB2 udf

Dear all,

I have encountered a problem in passing a VARCHAR parameter (which is a
list of accepted values) to the IN predicate of a DB2 user-defined
function.

For example, I have a table (mytable) storing ppl names & their
corresponding groups. I would like to create a UDF (myfunc) that select
a list of ppl names from (mytable) who belong to some dynamic choices
of groups. I have tried the following codings but it doesn't work if I
try to provide a list of designated choices of groups as a single
parameter.

Can anyone help? Thanks a lot!

Tracy

////Reference SQLs:

CREATE TABLE mytable (
name VARCHAR(10),
group CHAR(1)
);

INSERT into mytable values ('Peter','A');
INSERT into mytable values ('John','A');
INSERT into mytable values ('Mary','B');
INSERT into mytable values ('Susan','C');

CREATE FUNCTION myfunc (cgroup VARCHAR(20))
RETURNS TABLE
(
name VARCHAR(10)
)

LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
RETURN

SELECT
name
FROM
mytable
where
group in (cgroup)
;
db2 "select * from mytable"
NAME GROUP
---------- -----
Peter A
John A
Mary B
Susan C

4 record(s) selected.
db2 "select * from table(myfunc(char('A'))) as t1"
NAME
----------
Peter
John

2 record(s) selected.
db2 "select * from table(myfunc(char('A,B'))) as t1"
NAME
----------

0 record(s) selected.
db2 "select * from table(myfunc(char('''A'',''B'''))) as t1"


NAME
----------

0 record(s) selected.

Mar 30 '06 #1
10 3248
Tracy wrote:
Dear all,

I have encountered a problem in passing a VARCHAR parameter (which is a
list of accepted values) to the IN predicate of a DB2 user-defined
function.

For example, I have a table (mytable) storing ppl names & their
corresponding groups. I would like to create a UDF (myfunc) that select
a list of ppl names from (mytable) who belong to some dynamic choices
of groups. I have tried the following codings but it doesn't work if I
try to provide a list of designated choices of groups as a single
parameter.

Can anyone help? Thanks a lot!

Tracy

////Reference SQLs:

CREATE TABLE mytable (
name VARCHAR(10),
group CHAR(1)
);

INSERT into mytable values ('Peter','A');
INSERT into mytable values ('John','A');
INSERT into mytable values ('Mary','B');
INSERT into mytable values ('Susan','C');

CREATE FUNCTION myfunc (cgroup VARCHAR(20))
RETURNS TABLE
(
name VARCHAR(10)
)

LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
RETURN

SELECT
name
FROM
mytable
where
group in (cgroup)
;
db2 "select * from mytable"


NAME GROUP
---------- -----
Peter A
John A
Mary B
Susan C

4 record(s) selected.
db2 "select * from table(myfunc(char('A'))) as t1"


NAME
----------
Peter
John

2 record(s) selected.
db2 "select * from table(myfunc(char('A,B'))) as t1"


NAME
----------

0 record(s) selected.
db2 "select * from table(myfunc(char('''A'',''B'''))) as t1"

The in list sees the parameter as a single VARCHAR, not as a list of
strings. Th eeasiest way to get where you want to be is to use dynamic
SQL which is allowed in SQL Procedures only:

CREATE PROCEDURE myproc(IN cgroup VARCHAR(20))
BEGIN
DECLARE curtxt VARCHAR(1000);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR WITH RETURN FOR stmt;
SET curtxt = 'SELECT * FROM mytab WHERE group IN (' || cgroup || ')';
PREPARE stmt;
OPEN cur;
END

CALL myproc('''A'', ''B''')

If your groups have a fixed length (say single character for simplicity)
and you want to stick with pure SQL you can try this:

CREATE FUNCTION myfun(cgroup VARCHAR(20))
RETURNS TABLE(name VARCHAR(10))
RETURN
WITH rec(name, num)
AS SELECT name, 1
FROM mytab WHERE group = substr(cgroup, 1, 1)
UNION ALL
SELECT mytab.name, num + 1
FROM mytab, rec
WHERE group = substr(cgroup, NULLIF(num+1, LENGTH(cgroup)), 1)
SELECT name from rec;

SELECT * FROM TABLE(myfun('AB')) AS F;

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 30 '06 #2
Serge Rielau wrote:
Tracy wrote:
Dear all,

I have encountered a problem in passing a VARCHAR parameter (which is a
list of accepted values) to the IN predicate of a DB2 user-defined
function.

For example, I have a table (mytable) storing ppl names & their
corresponding groups. I would like to create a UDF (myfunc) that select
a list of ppl names from (mytable) who belong to some dynamic choices
of groups. I have tried the following codings but it doesn't work if I
try to provide a list of designated choices of groups as a single
parameter.

Can anyone help? Thanks a lot!

Tracy

////Reference SQLs:

CREATE TABLE mytable (
name VARCHAR(10),
group CHAR(1)
);

INSERT into mytable values ('Peter','A');
INSERT into mytable values ('John','A');
INSERT into mytable values ('Mary','B');
INSERT into mytable values ('Susan','C');

CREATE FUNCTION myfunc (cgroup VARCHAR(20))
RETURNS TABLE
(
name VARCHAR(10)
)

LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
RETURN

SELECT
name
FROM
mytable
where
group in (cgroup)
;
db2 "select * from mytable"


NAME GROUP
---------- -----
Peter A
John A
Mary B
Susan C

4 record(s) selected.
db2 "select * from table(myfunc(char('A'))) as t1"


NAME
----------
Peter
John

2 record(s) selected.
db2 "select * from table(myfunc(char('A,B'))) as t1"


NAME
----------

0 record(s) selected.
db2 "select * from table(myfunc(char('''A'',''B'''))) as t1"

The in list sees the parameter as a single VARCHAR, not as a list of
strings. Th eeasiest way to get where you want to be is to use dynamic
SQL which is allowed in SQL Procedures only:

CREATE PROCEDURE myproc(IN cgroup VARCHAR(20))
BEGIN
DECLARE curtxt VARCHAR(1000);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR WITH RETURN FOR stmt;
SET curtxt = 'SELECT * FROM mytab WHERE group IN (' || cgroup || ')';
PREPARE stmt;
OPEN cur;
END

CALL myproc('''A'', ''B''')

If your groups have a fixed length (say single character for simplicity)
and you want to stick with pure SQL you can try this:

CREATE FUNCTION myfun(cgroup VARCHAR(20))
RETURNS TABLE(name VARCHAR(10))
RETURN
WITH rec(name, num)
AS SELECT name, 1
FROM mytab WHERE group = substr(cgroup, 1, 1)
UNION ALL
SELECT mytab.name, num + 1
FROM mytab, rec
WHERE group = substr(cgroup, NULLIF(num+1, LENGTH(cgroup)), 1)
SELECT name from rec;

SELECT * FROM TABLE(myfun('AB')) AS F;

Oops:
NULLIF(num+1, LENGTH(cgroup) + 1)

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 30 '06 #3
Even without fixed length, as long as comma is not a valid character:

CREATE FUNCTION myfun(cgroup VARCHAR(20)) RETURNS TABLE(name
VARCHAR(10))
RETURN SELECT name FROM mytable WHERE POSSTR(',' || cgroup || ',',
group) > 0

Well, it's untested (i can't make a FUNCTION on a temp table.)

B.

Mar 30 '06 #4
A separate issue.

Are you sure you want the FUNCTION to be DETERMINISTIC?

B.

Mar 30 '06 #5
Brian,

I guess use of POSSTR wouldn't be easy if Tracy has longer/complex
strings. She may even need "ESCAPE" s...

Regards,

Mehmet

Mar 30 '06 #6
This is a common Newbie design error, usually comitted by people who
learned to program in BASIC instead of a compiled language. How many
parameters can a proceure accept? I don't the limit in DB@, but SQL
Server can have 1024 of them.

CREATE FUNCTION myfunc (cgroup VARCHAR(20))
RETURNS TABLE
(IN foobar_ name VARCHAR(10),
IN p1 INTEGER, IN p2 INTEGER, ..IN pn INTEGER)
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
RETURN
SELECTmfoobar_name
FROM Foobar
WHERE grp_nbr IN (p1, p2, .., pn);

This will avoid dynamic SQL and all the horrible problems you have with
it. You can do this parameter list with a simple text editor.

Mar 31 '06 #7
Dear Serge, thanks so much for your advice. I would like to stick to
pure SQL this time and my groups are of variable lengths. Anyway, your
suggestion are truly useful for my future reference. =D

Mar 31 '06 #8
Dear Brian, i am actually quite confused on when to use DETERMINISTIC
or NOT DETERMINISTIC (rather sily :P), any idea on this case?

Mar 31 '06 #9
On this case? Do not use it.
From the manual:


===============
This optional clause specifies whether the function always returns the
same results for given argument values (DETERMINISTIC) or whether the
function depends on some state values that affect the results (NOT
DETERMINISTIC). That is, a DETERMINISTIC function must always
return the same table from successive invocations with identical
inputs.
Optimizations taking advantage of the fact that identical inputs always
produce the same results are prevented by specifying NOT
DETERMINISTIC.
NOT DETERMINISTIC must be explicitly or implicitly specified if the
body of the function accesses a special register or calls another
non-deterministic function (SQLSTATE 428C2).
===============

Basically, if the data can change, assume the data will change. And if
it changes, DETERMINISTIC tells the FUNCTION not to care. So, if a
FUNCTION uses a TABLE, DETERMINISTIC should not be used.

B.

Mar 31 '06 #10
Oops. The IN paramater needs the commas too.

CREATE FUNCTION myfun(cgroup VARCHAR(20)) RETURNS TABLE(name
VARCHAR(10))
RETURN SELECT name FROM mytable WHERE POSSTR(',' || cgroup || ',', ','
|| group || ',') > 0

B.

Mar 31 '06 #11

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

Similar topics

10
by: Resant | last post by:
I have a query : Exec 'Select * From Receiving Where Code In (' + @pCode + ')' @pCode will contain more than one string parameter, eg : A1, A2, A3 How can i write that parameters, I try use :...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
9
by: Martoon | last post by:
I want to instantiate an STL map with my own compare function, and I want to pass a parameter to the compare function that will be stored and used for all comparisons in that map instance. As an...
10
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
4
by: Deckarep | last post by:
Hello fellow C# programmers, This question is more about general practice and convention so here goes: I got into a discussion with a co-worker who insisted that as a general practice all...
3
by: John Dalberg | last post by:
I have seen examples for List<T>.FindAll(findthis)where findthis is a predicate. How do I pass a parameter to this predicate so that I have different values to search for? I don't want to use...
7
by: =?ISO-8859-2?Q?Gregor_Kova=E8?= | last post by:
Hi! Is it possible to use parameter markers like this: 1.) SELECT * FROM TABLE1 WHERE FIELD1 LIKE '%?%' If I now set parameter 1 to '' (empty string) I don't get any rows back, but if I run:...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.