Solved

How to implement Ref cursor on DB2

Posted on 2007-11-29
4
2,431 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Creating and Managing Databases with phpMyAdmin in cPanel.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to recover a database from a user managed backup

770 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