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 > Function Generic

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-08-11, 10:37
kbum kbum is offline
Registered User
 
Join Date: Mar 2011
Posts: 12
Question Function Generic

Hello. I have a problem with functions. I need to create a function where i give the params, table name, schema name and field name. Is a function that will go get the value of a field of parameter.

Example:
CREATE FUNCTION "SCHEMA_FUNC".FNC_GET_ID (
SCHEMA_NAME VARCHAR(50),
TABLE_NAME VARCHAR(50),
FIELD_NAME VARCHAR(50))
RETURNS TABLE ( FIELD_NAME INTEGER )
RETURN SELECT FIELD_NAME
FROM SCHEMA_NAME.TABLE_NAME
COMMIT;

SELECT * FROM TABLE("SCHEMA_FUNC".FNC_GET_ID('TEST', 'USER', 'ID_USER'))

Thank you.
Reply With Quote
  #2 (permalink)  
Old 04-08-11, 12:42
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You cannot do that directly with a SQL function. You can try doing the "query" in a stored procedure and then wrap the SP with the function.

Andy
Reply With Quote
  #3 (permalink)  
Old 04-08-11, 13:16
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
What is the advantage of (a) over (b)?

(a) SELECT * FROM TABLE("SCHEMA_FUNC".FNC_GET_ID('TEST', 'USER', 'ID_USER'))

(b) SELECT ID_USER FROM TEST.USER
Reply With Quote
  #4 (permalink)  
Old 04-08-11, 13:32
kbum kbum is offline
Registered User
 
Join Date: Mar 2011
Posts: 12
It was just an example code that I quoted. The idea is to reuse code. When there is a need for maintenance, the idea is to have to make corrections in just one place. Another example would be the following:

CREATE FUNCTION "SCHEMA_FUNC".FNC_GET_ID (
SCHEMA_NAME VARCHAR(50),
TABLE_NAME VARCHAR(50),
FIELD_NAME VARCHAR(50))
RETURNS TABLE ( FIELD_NAME INTEGER )
RETURN SELECT MAX(FIELD_NAME)+1
FROM SCHEMA_NAME.TABLE_NAME
COMMIT;

SELECT * FROM TABLE("SCHEMA_FUNC".FNC_GET_ID('TEST', 'USER', 'ID_USER'))

My system currently has over 2000 tables. And if for some reason I wanted to be the id MAX + 2? I would have too big a job, having to go through all the tables to correct.
Reply With Quote
  #5 (permalink)  
Old 04-08-11, 20:30
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Quote:
And if for some reason I wanted to be the id MAX + 2? I would have too big a job, having to go through all the tables to correct.
won't it be better to put max+1 in as a scalar function, rather than trying to call udf which calls stored procs.
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
Reply

Tags
function, generic

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