| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

11-07-08, 11:41
|
|
Registered User
|
|
Join Date: Nov 2008
Posts: 1
|
|
|
SQL Function with select stmt that uses parameter from function
|
|
Hello,
I have encountered the following problem while migrating some functions from postgres to db2.
I want to write a function like this:
create function myfunc( tablename varchar(30))
RETURNS varchar(4000) LANGUAGE SQL READS SQL DATA NO EXTERNAL ACTION DETERMINISTIC
begin atomic
for row as
select * from $tablename$
do
...
end for;
return sth.
end;
My problem is, that I have no idea how to use the parameter "tablename" in this sql statement you find in the for clause.
In the PostGres Function I built a string representing my select statement and executed it to iterate over the result.
Guess it's easy for someone who knows DB2 much better.
Any ideas?
Have a nice weekend.
Marcus
|
|

11-07-08, 12:01
|
|
:-)
|
|
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
|
|
I don't think you can do it in an SQL function, but you can use dynamic SQL in a stored procedure, which in turn can be called from the function. Search documentation for "dynamic sql".
|
|

11-07-08, 14:16
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 39
|
|
|
|
You can try it as follows.
create function func1(tablename varchar(50))
return........
begin atomic
declare cmd varchar(200);
SET cmd = 'SELECT * FROM' ||tablename;
PREPARE stmt from cmd;
DECLARE c1 cursor for stmt;
open c1;
fetch.........
END@
Thanks,
Madhavi
|
|

11-07-08, 15:09
|
|
:-)
|
|
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
|
|
Quote:
|
Originally Posted by madhu_kaza
You can try it as follows.
|
Clearly, you haven't tried it yourself. Cursors (and related statements) are not supported in user-defined SQL functions.
|
|

11-08-08, 05:04
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 39
|
|
yea, you cannot use cursors or prepare statements in a CLP code, but you can use them in SQC(Embedded C).
you can refer to udfcli.sqc sample in sqllib/samples/c directory.
Thanks,
Madhavi
|
|

11-08-08, 07:14
|
|
:-)
|
|
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
|
|
Quote:
|
Originally Posted by madhu_kaza
yea, you cannot use cursors or prepare statements in a CLP code, but you can use them in SQC(Embedded C).
|
Certainly. You may have noticed though that we are talking about SQL functions here, not CLP or C functions.
|
|

11-09-08, 16:24
|
|
Registered User
|
|
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
|
|
You may be able to put this code into a stored procedure and call the procedure from a UDF.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
|
|

11-10-08, 02:02
|
|
Registered User
|
|
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
|
|
Quote:
|
Originally Posted by stolze
You may be able to put this code into a stored procedure and call the procedure from a UDF.
|
You may even let that stored procedure just declare and open the cursor, and return a handle to the cursor, so that the fetches happen in the caller.
Not sure though whether a UDF is able to do the FETCHes.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
|
|

11-11-08, 03:40
|
|
Registered User
|
|
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
|
|
No, a UDF can't do that. :-(
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|