Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 12th, 2005, 10:09 AM
Fred BROUARD - SQLpro
Guest
 
Posts: n/a
Default UDF with dynamic SQL and result

Hi,

unfortunately I have no current DB2 server to try this code :

CREATE FUNCTION FN_HOW_MANY_ROWS (TableName VARCHAR(128))
RETURNS INTEGER
LANGUAGE SQL
PARAMETER STYLE SQL
SPECIFIC HowManyRows
DETERMINISTIC
RETURNS NULL ON NULL INPUT
READS SQL DATA
BEGIN
DECLARE SQL_string VARCHAR(1000);
SET SQL_string = 'SELECT COUNT(*) FROM ' || TableName;
RETURN EXECUTE IMMEDIATE SQL_string;
END

Is this possible, or if it is not the right way to di it, how can I have a UDF
that return the number of rows of a table wich the name is a parameter ?

Thanks.

Fred

  #2  
Old November 12th, 2005, 10:10 AM
Serge Rielau
Guest
 
Posts: n/a
Default Re: UDF with dynamic SQL and result

Couple of changes:
1. In DB2 V8.2 put the code into a procedure then
create a function that CALLs the procedure
2. SET statement is not dynamic.
You need to use a cursor ofer one row.

CREATE PROCEDURE p(..) ..do fancy stuff
CREATE FUNCTION foo(..)
...
BEGIN ATOMIC
DECLARE ret ..;
CALL p(..., ret);
RETURN ret;
END

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles