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 > Generic external UDF function written in C

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-29-07, 17:12
kfwolf kfwolf is offline
Registered User
 
Join Date: Mar 2007
Posts: 5
Generic external UDF function written in C

Is there a way to develop an external UDF template such that it takes a (IN parameter) varchar and returns a table. This assumes the varchar is in the form of a select statement. The returned table may have to be altered to accommodate the columns in the select statement.

For example:

In param = "select colA from tableA where id = 7"

Returning table would have a single column of data type that resembles col A.

The goal is to make a simple UDF that returns the result set from the statement that it is passed.
Reply With Quote
  #2 (permalink)  
Old 03-30-07, 06:33
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
No .. When defining a table UDF, you have to define the output table structure , eg .
CREATE FUNCTION MYFUNC()
RETURNS TABLE (TIMERECEIVED DATE,
SUBJECT VARCHAR(15),
SIZE INTEGER,
TEXT VARCHAR(30)

Is there a reason you do not want to use a Stored Proc ???

HTH

Sathyaram

PS: This is your third post with the same question. Please do not start another one.
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 03-30-07, 11:44
kfwolf kfwolf is offline
Registered User
 
Join Date: Mar 2007
Posts: 5
Question Defining output structure

Maybe I was not clear with my question...

Lets assume the returned table is a single column for discussion purposes.

So, is it possible to return the result set from a query that is passed as varchar?

BTW, this is my first post on this subject. Could you direct us to the 2 other posts that have been opened? And hopefully answered.
Reply With Quote
  #4 (permalink)  
Old 03-30-07, 13:16
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Your question is : Can you pass a query as input to a UDF and get the result set of the query as output of table function.

The simple answer is Yes. (Disclaimer: AFAIK … I have not tried defining external table function myself)

There are a few more points to remember though:

a) your resultset column names will be as per your UDF definition and NOT as per your input query.
b) The number and datatypes of the resultset columns is fixed at the UDF definition time.

As example for above ,
If you define your UDF :

CREATE FUNCTION MYFUNC(QRY VARCHAR(1000))
RETURNS TABLE (TIMERECEIVED DATE,
SUBJECT VARCHAR(15)

)


Then your call,
Select * from table (‘myfunc(select date(timestampcol) as d,namecol as n from employee’) as udf

Then your resultset will have the column names TIMERECEIVED and SUBJECT and not D and N.

You call cannot be :
Select * from table (‘myfunc(select date(timestampcol) as d,namecol as n , salarycol from employee’) as udf


Have a read through


http://publib.boulder.ibm.com/infoce...n/r0000917.htm
PS: My apologies, your earlier post had a slightly different question .. I misundersood
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 03-31-07, 13:00
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by sathyaram_s
Your question is : Can you pass a query as input to a UDF and get the result set of the query as output of table function.

The simple answer is Yes. (Disclaimer: AFAIK … I have not tried defining external table function myself)
You can remove the AFAIK. It is definitively possible. As we explained extensively before (primarily in the DB2 newsgroup), there are a lot of examples shipped with DB2 that show how to implement table functions. You have to declare and open a cursor inside the table function in the OPEN call, and each FETCH call to the UDF translates to a FETCH on the cursor. When DB2 makes the SQLUDF_TF_CLOSE call to the table UDF, you close the cursor. It's simple as that - and exactly the same you would have to do when implementing a ResultSet class in Java if you were to write a JDBC driver.

You (the OP) should simply give it a try and if it does not work, ask specific questions...

Quote:
There are a few more points to remember though:
a) your resultset column names will be as per your UDF definition and NOT as per your input query.
That is not much of a problem because you can simply (re)name the columns of every table that occurs in the FROM clause:
Code:
SELECT ... FROM TABLE ( func(...) ) AS func_result_table(a, b, c)
or this for base tables, views, and nicknames:
Code:
SELECT ... FROM t1 AS t(a, b, c)
Quote:
b) The number and datatypes of the resultset columns is fixed at the UDF definition time.

As example for above , If you define your UDF :
Code:
CREATE FUNCTION MYFUNC(QRY VARCHAR(1000))
RETURNS TABLE (TIMERECEIVED DATE,
SUBJECT VARCHAR(15))
Then your call,
Code:
Select * from table (‘myfunc(select date(timestampcol) as d,namecol as n from employee’) as udf
Then your resultset will have the column names TIMERECEIVED and SUBJECT and not D and N.

You call cannot be :
Code:
Select * from table (‘myfunc(select date(timestampcol) as d,namecol as n , salarycol from employee’) as udf
Actually, you can provide such a query (with corrected positioning of the single-quotes). As long as the table function makes sure that only two columns are returned to DB2 and the third column is ignored. The only other criteria is that the data types match with whatever was defined at CREATE FUNCTION time, as you already pointed out.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development

Last edited by stolze; 03-31-07 at 13:06.
Reply With Quote
  #6 (permalink)  
Old 04-04-07, 17:05
kfwolf kfwolf is offline
Registered User
 
Join Date: Mar 2007
Posts: 5
Function error

Sathyaram:

I took your suggestion - trying to learn UDFs.

Modifying the function slightly:

CREATE FUNCTION userid.MYFUNC(QRY VARCHAR(1000))
RETURNS TABLE (med varchar(128)
)

Gave me an error:

SQL0491N The definition of routine userid.myfunc must have a returns clause, and one of: the external clause (with other required keywords); the RETURN statement; or the source clause. SQLSTATE = 42601

I assume this is because we are not actually returning anything.

Therefore modified the query as such - - just to return a value:

CREATE FUNCTION userid.MYFUNC(QRY VARCHAR(1000))
RETURNS TABLE (result varchar(128)
)

begin atomic
return
select med from systools.policy;
end
;

However - i am looking to execute qry.

The method to call the function would be from another function or procedure. In fact, this could be a procedure as well. Select * from myfunc (squery); - OR - Call myproc (squery) to return a list of varchar

At this point and time, my original assignment is completed. I am just seeing if DB2 has the capabilities within a SQL UDF or external C UDF. This is now more of a personal R&D objective - will try Knut's cursor suggestion and post any errors that I run into.
Reply With Quote
  #7 (permalink)  
Old 04-10-07, 15:25
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
No, the SQL0491 error is because you did not provide a valid CREATE FUNCTION statement. Have a look at the syntax diagram in the manual. You will see that either an EXTERNAL NAME clause (for external table functions), a RETURN clause (for SQL-bodied functions), or a SOURCE clause (for sourced table functions) is mandatory.
__________________
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