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

  • Home
  • SEARCH
  • 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 8707873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:58:23+00:00 2026-06-13T03:58:23+00:00

IN my oracle database i want to create a function or procedure with cursor

  • 0

IN my oracle database i want to create a function or procedure with cursor which will use dynamic table name.here is my code.

CREATE OR REPLACE Function Findposition ( model_in IN varchar2,model_id IN number) RETURN number IS cnumber number;
TYPE c1 IS REF CURSOR;
c2 c1;
BEGIN
open c2 FOR 'SELECT id,ROW_NUMBER() OVER ( ORDER BY id) AS rownumber FROM '||model_in;
FOR employee_rec in c2
LOOP
    IF employee_rec.id=model_id then
    cnumber :=employee_rec.rownumber;
    end if;
END LOOP;
close c2;
RETURN cnumber;
END;

help me to solve this problem.IN

  • 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-06-13T03:58:25+00:00Added an answer on June 13, 2026 at 3:58 am
    • There is no need to declare a c1 type for a weakly typed ref cursor. You can just use the SYS_REFCURSOR type.
    • You can’t mix implicit and explicit cursor calls like this. If you are going to OPEN a cursor, you have to FETCH from it in a loop and you have to CLOSE it. You can’t OPEN and CLOSE it but then fetch from it in an implicit cursor loop.
    • You’ll have to declare a variable (or variables) to fetch the data into. I declared a record type and an instance of that record but you could just as easily declare two local variables and FETCH into those variables.
    • ROWID is a reserved word so I used ROWPOS instead.

    Putting that together, you can write something like

    SQL> ed
    Wrote file afiedt.buf
    
      1  CREATE OR REPLACE Function Findposition (
      2      model_in IN varchar2,
      3      model_id IN number)
      4    RETURN number
      5  IS
      6    cnumber number;
      7    c2      sys_refcursor;
      8    type result_rec is record (
      9      id      number,
     10      rowpos  number
     11    );
     12    l_result_rec result_rec;
     13  BEGIN
     14    open c2 FOR 'SELECT id,ROW_NUMBER() OVER ( ORDER BY id) AS rowpos FROM '||model_in;
     15    loop
     16      fetch c2 into l_result_rec;
     17      exit when c2%notfound;
     18      IF l_result_rec.id=model_id
     19      then
     20        cnumber :=l_result_rec.rowpos;
     21      end if;
     22    END LOOP;
     23    close c2;
     24    RETURN cnumber;
     25* END;
    SQL> /
    
    Function created.
    

    I believe this returns the result you expect

    SQL> create table foo( id number );
    
    Table created.
    
    SQL> insert into foo
      2    select level * 2
      3      from dual
      4   connect by level <= 10;
    
    10 rows created.
    
    SQL> select findposition( 'FOO', 8 )
      2    from dual;
    
    FINDPOSITION('FOO',8)
    ---------------------
                        4
    

    Note that from an efficiency standpoint, you’d be much better off writing this as a single SQL statement rather than opening a cursor and fetching every row from the table every time. If you are determined to use a cursor, you’d want to exit the cursor when you’ve found the row you’re interested in rather than continuing to fetch every row from the table.

    From a code clarity standpoint, many of your variable names and data types seem rather odd. Your parameter names seem poorly chosen– I would not expect model_in to be the name of the input table, for example. Declaring a cursor named c2 is also problematic since it is very non-descriptive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a stored procedure on ORACLE database server and my problem
Folks, I have a oracle database table which has name value pairs. A set
I want to take backup or restore of my oracle database through .Net code.
I have 3 columns in Oracle database having table mytable and i want records
I have a table in Oracle 11.2 database. I want the database to run
I want to create a Spoon transformation which will work on multiple values of
I want to create an insert trigger on MySQL which will automatically insert the
I have a oracle database with a table tha contains user. I want to
I want to use an Oracle database with c#. Please suggest an Oracle version
hi to all I insert into Oracle database image file on Delphi(create table (id

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.