0

I have a stored procedure which I want to return cursor as the OUT parameter. I see the output in dbms_output.put_line(as_return_val); like

Loan Purpose can not be Limited ||  /loanPurpose 
THE PURPOSE OF PURCHASE IS NOT VALID ||  /loanPurpose 

I need to read this response as a array of Objects(each ro one Object) in my service layer. Sorry I am not sure I need to send it as a cursor or like a varchar from here. I added as_return_val this variable to see whether I am getting correct response .

Procedure

create or replace PROCEDURE PR_LOGIC_CHECK_TEST 
(in_loan_id IN NUMBER,
in_trans_id  IN NUMBER)AS 

BEGIN
DECLARE
as_errm          VARCHAR2(2000);
curr_cursor_out  SYS_REFCURSOR;
temp_cursor_row  temp_cor_ll_cursor%rowtype;
as_return_val    VARCHAR2(500);
BEGIN
pr_loan_logic_check(in_loan_id, in_trans_id, as_errm,  curr_cursor_out);
LOOP
    FETCH curr_cursor_out INTO temp_cursor_row;
    EXIT WHEN curr_cursor_out%notfound;
    as_return_val := temp_cursor_row.ret_value
                     || ' ||  '
                     || '/'||temp_cursor_row.xpath_name;

    --dbms_output.put_line(as_return_val);
END LOOP;
close curr_cursor_out;
ROLLBACK;
END;
END PR_LOGIC_CHECK_TEST;
4
  • I'm not sure that I understand what you want to happen. You have declared temp_cursor_row as a sys_refcursor in your procedure spec despite the name implying that you want it to be a row. And then you've declared a local variable of the same name but a different data type in a nested PL/SQL block. That doesn't make sense. My first guess is that you just want to return the cursor from pr_loan_logic_check in which case your procedure would merely call pr_loan_logic_check passing in temp_cursor_row (the parameter) rather than curr_cursor_out Commented Apr 3, 2021 at 3:38
  • My next guess is that you want to insert the as_return_val value that you generate inside the loop into a different table (perhaps a temporary table) and then open temp_cursor_row (the parameter not the local variable) for a query against that table. Commented Apr 3, 2021 at 3:40
  • A reproducible example here would very helpful. If you provided us with one and showed the output you want given that example, it would likely make the question much clearer. Commented Apr 3, 2021 at 3:41
  • Sorry for the confusion . I just updated the question. Again i am not 100% sure how i need to send the response back from SP either as a cursor or as a varchar . I just added as_return_val this local variable to see whether i am getting the expected response or not . All i need is those shown response as a output from SP. Commented Apr 3, 2021 at 3:55

1 Answer 1

1

I'm still guessing at what you really want to happen since you haven't actually given us a reproducible test case (i.e. something that we can run locally or on dbfiddle or liveSQL that produces a particular output given a particular input).

Architecturally, it seems problematic to have a stored procedure whose only purpose is to format the output of a different procedure for human consumption. Formatting results would more properly be done in the display layer (the view of an MVC application but view means something else when we're talking about databases so I'm using "display layer") of the application not in a stored procedure. It would make more sense for your application to call pr_loan_logic_check directly and to let your display layer decide to concatenate the values from multiple columns together. If someone wants to change how the output is formatted later, you'd then just be changing code in the display layer not in your backend database.

My guess is that you want to return a collection like this. Note that I'm creating the collection type at the SQL level. You could create it in a PL/SQL package as well.

create or replace type varchar2_tbl
  is table of varchar2(500);

create or replace PROCEDURE PR_LOGIC_CHECK_TEST (
  in_loan_id   IN NUMBER,
  in_trans_id  IN NUMBER,
  out_strings OUT varchar2_tbl
)
AS 
  as_errm          VARCHAR2(2000);
  curr_cursor_out  SYS_REFCURSOR;
  temp_cursor_row  temp_cor_ll_cursor%rowtype;
BEGIN
  pr_loan_logic_check(in_loan_id, in_trans_id, as_errm,  curr_cursor_out);

  out_strings := out_strings();
  LOOP
    FETCH curr_cursor_out INTO temp_cursor_row;
    EXIT WHEN curr_cursor_out%notfound;

    out_strings.extend();
    out_strings( out_strings.count ) := 
            temp_cursor_row.ret_value
         || ' ||  '
         || '/'||temp_cursor_row.xpath_name;
  END LOOP;
  close curr_cursor_out;

  -- I have trouble imagining why you'd put a `rollback` here
  ROLLBACK;
END PR_LOGIC_CHECK_TEST;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks , The only thing i didn't understand is where i need to define that collection type . varchar2_tbl .
@Vinoy - I defined it in SQL in my answer-- wherever you're running the create procedure statement, you'd just first run the create type statement assuming you don't already have a type declaration for a nested table of strings. If you don't want to define the type in SQL, you could also define it in PL/SQL by putting it in a package declaration. If your actual procedure is already in a package (almost all procedures ought to be in packages), you could define the nested table type in that package spec.
Thank You for the input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.