Oracle User-Defined Function

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
vick
Participant
Posts: 88
Joined: Sun Oct 30, 2005 2:06 am

Oracle User-Defined Function

Post by vick »

Hello,

I have created a user-defined function like the one below.

Code: Select all


CREATE OR REPLACE FUNCTION GET_FUNCTION(ENG_DIS IN VARCHAR2)
RETURN VARCHAR2 IS
col VARCHAR2(50);
BEGIN
SELECT XYZ_DESCRIPTION INTO col
FROM TBL_NAME
WHERE ROWNUM < 2;
RETURN col;
END GET_FUNCTION;
/
show errors;

I understand that a FUNCTION returns just "a value".

If I execute the function in toad for testing it returns XYZ_DESCRIPTION and about 6000 rows (same as the no of rows in table "TBL_NAME").

SQL Query in TOAD

Code: Select all


SELECT GET_FUNCTION('XYZ_DESCRIPTION ')
FROM TBL_NAME

Any pointers?
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Add a 'where' clause. :?

Is this just a... 'play' function?
-craig

"You can never have too many knives" -- Logan Nine Fingers
vick
Participant
Posts: 88
Joined: Sun Oct 30, 2005 2:06 am

Post by vick »

Not really a play function. I will add more functionalities to it.

I plan to use Substr(Instr(XYZ_DESCRIPTION).

If I add a where clause

WHERE XYZ_DESCRIPTION = ' aaaa bbbb cccc'

I get this
  • ORA-12801: error signaled in parallel query server P000
    ORA-29913: error in executing ODCIEXTTABLEFETCH callout
    ORA-01422: exact fetch returns more than requested number of rows
    ORA-06512: at "STAGE.GET_ENGINE_DISPLACEMENT", line 5
    ORA-06512: at "SYS.ORACLE_LOADER", line 52
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Where did you add that 'where'? You'd need to talk to your DBA about the nature of those errors.

I only ask if it was real as it doesn't really make any sense as a function to me right now. A function is some sort of derived field that you select like any other field from any table - yours will always return 1 particular value from 1 particular table, hence my confusion.

You also seem to be duplicating functionality already inside DataStage.
-craig

"You can never have too many knives" -- Logan Nine Fingers
vick
Participant
Posts: 88
Joined: Sun Oct 30, 2005 2:06 am

Post by vick »

I m trying to incorporate a PL/SQL user-defined function in Datastage as I have been asked to have C-routine as the last option for a task (that I tried with a tfr one-liner; reasons best known to the management).

Anyways I resolved the issue with the test function I posted.

Code: Select all


CREATE OR REPLACE FUNCTION GET_FUNCTION(ENG_DIS IN VARCHAR2) 
RETURN VARCHAR2 IS 
col VARCHAR2(50); 
BEGIN 
SELECT SUBSTR(XYZ_DESCRIPTION, INSTR(XYZ_DESCRIPTION, ' L ', -7), 9) INTO col 
FROM TBL_NAME 
WHERE ROWNUM < 2; 
RETURN col; 
END GET_FUNCTION; 
/ 
show errors; 

I was passing the argument within 'quotes' while calling the function.

Code: Select all


SELECT GET_FUNCTION(XYZ_DESCRIPTION) 
FROM TBL_NAME 

I was wondering if someone could help me with "call function()" within Datastage.

TIA
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Functions can not be CALLed. Only stored procedures can be called.

Use your function in a SELECT statement, perhaps one that selects only one row (maybe from DUAL).
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Get it working outside DataStage first. Once you have a working function,then you can call it inside the database stage wrapped in a sql.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
vick
Participant
Posts: 88
Joined: Sun Oct 30, 2005 2:06 am

Post by vick »

Thanks Ray, DSguru2B!

The function gets compiled with NO errors. I run the following query in TOAD and it runs just about fine.

Code: Select all

SELECT GET_FUNCTION(XYZ_DESCRIPTION) 
FROM TBL_NAME 
WHERE ROWNUM < 2
But if I use the DUAL table (as Ray suggested) it gives me an error saying XYZ_DESCRIPTION:Invalid identifier.

Code: Select all


SELECT GET_FUNCTION(XYZ_DESCRIPTION) 
FROM DUAL
WHERE ROWNUM <2 


Please suggest.

TIA[/b]
jhmckeever
Premium Member
Premium Member
Posts: 301
Joined: Thu Jul 14, 2005 10:27 am
Location: Melbourne, Australia
Contact:

Post by jhmckeever »

Your first post correctly contained quotes around the XYZ_DESCRIPTION value. The latest example doesn't ... but should
<b>John McKeever</b>
Data Migrators
<b><a href="https://www.mettleci.com">MettleCI</a> - DevOps for DataStage</b>
<a href="http://www.datamigrators.com/"><img src="https://www.datamigrators.com/assets/im ... l.png"></a>
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Ray's advise to run on dual was for testing purposes. You will reference the table which has the column 'XYZ_DESCRIPTION'. Use this function inside the database stage. Use generated sql. Put the function in the derivation of 'XYZ_DESCRIPTION' in the column tab. Examine the sql to be like how you want it to be.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
splayer
Charter Member
Charter Member
Posts: 502
Joined: Mon Apr 12, 2004 5:01 pm

Post by splayer »

1) vick, I don't see you using the input parameter to the function, ENG_DIS, anywhere. Is that what you want?

2)You get the ORA-01422 error because your where clause:

WHERE XYZ_DESCRIPTION = ' aaaa bbbb cccc'

is returning more than 1 single value rows and with a SELECT INTO, it should return one 1 value.

3)
SELECT GET_FUNCTION(XYZ_DESCRIPTION)
FROM DUAL
WHERE ROWNUM <2

You get an error in this one because Dual doesn't know the value of XYZ_DESCRIPTION. You have to either declare it as a variable and initialize its value or put it in single quotes when passing to the function.
vick
Participant
Posts: 88
Joined: Sun Oct 30, 2005 2:06 am

Post by vick »

Thanks DSguru2B.

This what I m doing.

Code: Select all

                          Oracle_Ref_Tbl
                               |
                               |
                               |  
Oracle_Source_Tbl------->LookupStage------->Tfr--------->Seq_File_Stage




Oracle Ref Tbl has following SQL query and I do a sparse look-up

Code: Select all

SELECT GET_FUNCTION(XYZ_DESCRIPTION)
FROM ABC
In the source table ABC(same table as the one used in the reference; SQL query above) I have 18 columns.

In the look up stage I add another column OUT_RESULT to the source (now 19 columns) and map that to the required filed in transformer output.

I get an error in the transformer that reads Error when checking operator:OUT_RESULT not found in input schema

Any pointers?

TIA
vick
Participant
Posts: 88
Joined: Sun Oct 30, 2005 2:06 am

Post by vick »

Sorry folks my bad. I resolved the issue.

I want to kick myself for my previous post. I did'nt include XYZ_DESCRIPTION in my Select statement.

Again I apologise.
narasimha
Charter Member
Charter Member
Posts: 1236
Joined: Fri Oct 22, 2004 8:59 am
Location: Staten Island, NY

Post by narasimha »

Good you found it out. Can you mark this post as resolved now :wink:
Narasimha Kade

Finding answers is simple, all you need to do is come up with the correct questions.
vick
Participant
Posts: 88
Joined: Sun Oct 30, 2005 2:06 am

Post by vick »

Hello folks,

Is it possible to do the following using PL/SQL user-defined function.

Code: Select all

  
1. "3.5 L V6 24-Valve DOHC" ---------------------> 3.5L 
2. "3.8 Liter V-6 24-Valve DOHC" ---------------------> 3.8L 
3. "4.6 Liter V8 w/4 valve per CYL" -------------------> 4.6L 
4. "5.5 L 32-Valve Aluminum V-12"----------------> 5.5L 
5. "5900 cc V-12 32-Valve Aluminum"----------------> 5900cc 
6. "AMG Modified 3.9 Liter V6 32-Valve Aluminum"--------------> 3.9L 
7. "AMG 5,439-cc 24-Valve Supercharged"-------> 5438cc 

TIA
Post Reply