Solved

How to implement Ref cursor on DB2

Posted on 2007-11-29
4
2,415 Views
Last Modified: 2008-09-17
Hi all

I need to translate an Oracle functions and Stores procedures that uses Ref Cursor datatypes variables, for example:

CREATE OR REPLACE FUNCTION    "EXISTE" (Selecc in Varchar2, Proced in Varchar2, Condic in Varchar2)
RETURN BOOLEAN
IS

Type Registro is Ref Cursor;
retorno   Registro;
p_valor1  int;
f_strsql  varchar2(4000);
p_existe  boolean;

Begin

p_valor1 := 0;
f_strsql := 'Select ' || Selecc || ' From ' || Proced || ' Where ' || Condic;
Open retorno for f_strsql;
Loop
  Fetch retorno into p_valor1;
  Exit when retorno%notfound;
End Loop;

If p_valor1 = 1 Then
  p_existe := True;
Else
  p_existe := False;
End If;

Return p_existe;

End EXISTE;
/

How i can do this on DB2?

Thanks a lot,
regards
0
Comment
Question by:alexalar
4 Comments
 
LVL 5

Accepted Solution

by:
ocgstyles earned 250 total points
ID: 20379414
Hi alexalar,

I'll start by noting I'm not familiar with Oracle's REF CURSOR, but from a quick read, it looks like a means to , in DB2's terms, return a table.  In addition to scalar values, DB2 functions can return tables.

Unfortunately, you are going to run into problems with that converting that function into DB2 function.  DB2 does not allow you to declare cursors in user-defined functions.  You can, however, use a FOR loop in a function to iterate through a result set.  Only problem though, is that you are using dynamic SQL, and you'll need a cursor for that.

With this sample Oracle function, I don't think you will get a direct replacement using DB2.  DB2 can produce the a similar result by using a stored procedure.  But a solution would be dependent on how your application is using the results and whether or not you can alter that.

Keith
0
 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 250 total points
ID: 20385282
hi
if you can' t define a cursor in a db2 user define function, you can create a stored procedure that will run the cursor you need, and call that stored procedure from a user defined function that will recieve the parameters and return value exactly like the code you wrote
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article describes some very basic things about SQL Server filegroups.
Creating and Managing Databases with phpMyAdmin in cPanel.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now