Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / February 2005

Tip: Looking for answers? Try searching our database.

UDF with dynamic SQL and result

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fred BROUARD - SQLpro - 05 Feb 2005 11:25 GMT
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
Serge Rielau - 05 Feb 2005 17:46 GMT
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
Signature

Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.