Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3874014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:06:12+00:00 2026-05-19T22:06:12+00:00

I have a Procedure in Oracle that takes a varchar2 paramater. Based on the

  • 0

I have a Procedure in Oracle that takes a varchar2 paramater. Based on the value of that parameter, I need to define a cursor. The cursor will operate on different tables based on the value of the parameter.

I wanted to do something like below but it throws an error in the CURSOR definition piece of code. Any ideas?

PROCEDURE GET_RECORDS(v_action IN VARCHAR2)
IS
CURSOR get_records
IS
       IF(v_action = 'DO THIS') THEN
           SELECT * from <THIS>;
       ELSE
           SELECT * from <THAT>;
       END IF;
BEGIN
       OPEN get_records;

       FETCH get_records
       INTO v_thing;

       v_loop := 0;
       WHILE get_records%FOUND
       LOOP

           FETCH get_records
           INTO v_thing;

       END LOOP;
       CLOSE get_records;
END;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:06:13+00:00Added an answer on May 19, 2026 at 10:06 pm

    you will need a REF CURSOR and open it conditionaly, for example:

    SQL> CREATE OR REPLACE PROCEDURE GET_RECORDS(v_action IN VARCHAR2) IS
      2     v_thing     VARCHAR2(10);
      3     get_records SYS_REFCURSOR;
      4  BEGIN
      5     IF (v_action = 'DO THIS') THEN
      6        OPEN get_records FOR
      7           SELECT 1 FROM dual;
      8     ELSE
      9        OPEN get_records FOR
     10           SELECT 2 FROM dual;
     11     END IF;
     12  
     13     LOOP
     14        FETCH get_records INTO v_thing;
     15        EXIT WHEN get_records%NOTFOUND;
     16        /* do things */
     17        dbms_output.put_line(v_thing);
     18     END LOOP;
     19     CLOSE get_records;
     20  END;
     21  /
    
    Procedure created
    
    SQL> exec get_records ('DO THIS');
    1
    
    PL/SQL procedure successfully completed
    
    SQL> exec get_records ('DO THAT');
    2
    
    PL/SQL procedure successfully completed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a Procedure in Oracle with a SYS_REFCURSOR output parameter that returns the
I have a stored procedure in Oracle that returns result set(s) via OUT parameter(s)
I have an Oracle stored procedure that returns a BLOB in an output parameter:
I have a situation where I have an Oracle procedure that is being called
I have the following iBatis mapping for an Oracle Stored Procedure that returns a
I have a Oracle procedure to which I have to pass a datetime value
I have an Oracle trigger which is calling a stored procedure that has PRAGMA
In Oracle, When we have a procedure that deletes a lot of data, I
I have an existing stored procedure that takes 2 parameters and returns back an
I have an Oracle stored procedure which takes two parameters: a custom data type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.