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.

 
Go Back  dBforums > Database Server Software > DB2 > SQL Function with select stmt that uses parameter from function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-07-08, 11:41
polarbaerhh polarbaerhh is offline
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
Reply With Quote
  #2 (permalink)  
Old 11-07-08, 12:01
n_i n_i is offline
:-)
 
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".
Reply With Quote
  #3 (permalink)  
Old 11-07-08, 14:16
madhu_kaza madhu_kaza is offline
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
Reply With Quote
  #4 (permalink)  
Old 11-07-08, 15:09
n_i n_i is offline
:-)
 
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.
Reply With Quote
  #5 (permalink)  
Old 11-08-08, 05:04
madhu_kaza madhu_kaza is offline
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
Reply With Quote
  #6 (permalink)  
Old 11-08-08, 07:14
n_i n_i is offline
:-)
 
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.
Reply With Quote
  #7 (permalink)  
Old 11-09-08, 16:24
stolze stolze is offline
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
Reply With Quote
  #8 (permalink)  
Old 11-10-08, 02:02
Peter.Vanroose Peter.Vanroose is offline
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/
Reply With Quote
  #9 (permalink)  
Old 11-11-08, 03:40
stolze stolze is offline
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On