Cursor declaration in packages.

Hi,
Our company is decided to use stored procedures for all database access from Java Servlets/JSP. We have to use lot of ref cursors. In a package there will be lots of procedures using cursor. So is it enough to declare only one cursor in the package head of a package and use it in all packages and procedures. Is there any sharing problem in doing it.
Anto Paul.

No, The purpose when you declare a cursosr in a package is that the resource can be shared for a lot of sessions maintain each sessions its private values. When a package is invoked the first time is loaded into the SGA and it makes faster the accesses to it.
Joel P�rez

Similar Messages

  • Declaration of cursor type in package/sp

    Hi,
    I'm using package with sp that using cursor like below:
    --#1============================ PACKAGE
    {PACKAGE                                ORA_PK_TR2 AS
      Type CURS_01  IS REF CURSOR;  --- return RYBB.T_COLLECT%rowtype;
      Procedure ORA_SP_CUST (EXP_DATE IN date,                    
    END
    --#2============================  BODY
    create or replace
    PACKAGE BODY                           ORA_PK_TR2  as
         Procedure ORA_SP_CUST (EXP_DATE IN date,                    
                                          open_CURS_01 OUT CURS_01 )   IS
    BEGIN
    SQL_string = '(select * from RYBB.T_COLLECT where col='||EXP_DATE)'
    OPEN open_CURS_01 FOR  SQL_STRING;
    END;
    --#3=============================  RUN_PORTION
    DECLARE
      EXP_DATE DATE;
    OPEN_CURS_01 RYBB.ORA_PK_TR2.CURS_01;
      TYPE_IN RYBB.T_COLLECT%ROWTYPE;       --/***** <==== need to move into package
    BEGIN
    EXP_DATE := '10-sep-10';
    RYBB.ORA_PK_TR2.ORA_SP_CUST(
        EXP_DATE => EXP_DATE,
       OPEN_CURS_01 => OPEN_CURS_01
    LOOP
        FETCH open_CURS_01 INTO TYPE_IN;
        EXIT WHEN open_CURS_01%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(TYPE_IN.COL1||' '||TYPE_IN.COL2);    --/ for sample
    END LOOP; 
    END;}
    I need to put TYPE_IN declation for cursor inside the package, so user who will run this pack/sp won't deal with this structure. How I can do this,
    I tried to use <return RYBB.T_COLLECT%rowtype;> in package but then I get :
    Error(122,6): PLS-00455: cursor 'open_CURS_01' cannot be used in dynamic SQL OPEN statement.
    Not sure can I put it somehow into the BODY and make it available for user ?
    Appreciate you help.
    BEst
    Trent
    Edited by: trento on Sep 13, 2010 2:36 PM

    --#1============================ PACKAGE
    PACKAGE ORA_PK_TR2 AS
    Type CURS_01 IS REF CURSOR; --- return RYBB.T_COLLECT%rowtype;
    Type CURS_01_TYP IS RYBB.T_COLLECT%rowtype;
    --#3============================= RUN_PORTION
    DECLARE
    EXP_DATE DATE;
    OPEN_CURS_01 RYBB.ORA_PK_TR2.CURS_01;
    TYPE_IN RYBB.ORA_PK_TR2.CURS_01_TYP;
    BEGIN
    Difficult to read, don't you think?
    Try use the tags B-)
    Your code has other issues and (as shown here) won't compile. If you have a working solution, why not paste that instead?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Variable declaration in Package and package body

    Hi
    Kindly reply the difference in declaring a cursor inside a package vs package body
    eg:
    create or replace package shapak is
    cursor shacur is select * from sha;
    function shafun return number;
    end;
    i can declare the above cursor inside the package body withoout declaring in package specification. but what is the difference?
    reply appreciated
    thanks
    shajan

    In general..Items declared in the package spec are visible outside the package..so you can say public.where as items declared in the body are restricted to use within the package..such items are called private.

  • Cursor in a Package

    Hi
    I declared a cursor in a package
    create
    PACKAGE DATA
    AS
    CURSOR C1_DATA IS SELECT * FROM EMP;
    c_data C1_DATA%ROWTYPE;
    procedure city;
    procedure state;
    procedure zip;
    END;
    I have all the records from emp(empid,empname,empcity,empstate,empzip) in the cursor. I need to write the package spec as
    create
    PACKAGE body DATA
    as
    procedure city..
    procedure state..
    procedure zip...
    END;
    I would like to write 3 different procedures for city,sate and zip update.So I need to fetch one record from cursor and then run 3 procedures for updating the same record.How can I achieve this
    fetch a record from cursor and then run all the procedures and fetch the next record and run all the procedurs on the same rec

    890563 wrote:
    Hi
    I wanted to write a package as
    create
    PACKAGE body DATA
    as
    fecth a record from the cursor to c_data
    procedure city
    begin update empcitytable set empcity = c_data.empcity where empid = c_data.empid;
    procedure state
    begin update empstatetable set empstate = c_data.empstate where empid = c_data.empid;
    procedure zip
    begin update empziptable set empzip = c_data.empzip where empid = c_data.empid;
    END;
    I would fetch a record from emp and update the same data in different tables.
    Edited by: 890563 on 23 Oct, 2012 12:16 AMWell so you update three different tables. Again the question is why cursor? I would just do 3 merge
    merge into empcitytable c
    using emp e
       on (e.empid = c.empid)
    when matched then
            update set c.empcity = e.empcity;
    merge into empstatetable c
    using emp e
       on (e.empid = c.empid)
    when matched then
            update set c.empstate= e.empstate;
    merge into empziptable c
    using emp e
       on (e.empid = c.empid)
    when matched then
            update set c.empzip = e.empzip;

  • Performance: Cursor declaration versus explicit query in BEGIN/END block

    Hi guys!
    Anyone knows if declare an explicit cursor inside a pl/sql block is faster than using a cursor declaration, and how fast it its?
    Which block runs faster? And how fast? ( once, twice, once and a half ? )
    Block1:
    DECLARE
    CURSOR cur_test (p1 NUMBER) IS
    SELECT field1, field2 FROM table WHERE field0 = p1;
    vf1 VARCHAR2(1)
    vf2 NUMBER;
    n NUMBER := 0;
    BEGIN
    OPEN cur_test ( n );
    FETCH cur_test INTO vf1, vf2;
    CLOSE cur_test;
    END;
    Block2:
    DECLARE
    vf1 VARCHAR2(1)
    vf2 NUMBER;
    n NUMBER := 0;
    BEGIN
    BEGIN
    SELECT field1, field2
    INTO vf1, vf2
    FROM table WHERE field0 = n;
    EXCEPTION
    WHEN others THEN
    null;
    END;
    END;
    I have LOOP in a cursor and may open/fetch/closes in this loop. I´m wondering how fast would it be if I change the open/fetch/closes to explicit query blocks...
    Thanks!
    Murilo

    If you expect your qurey to return a single row, you would generally want to use a SELECT ... INTO. You'd only want to use a cursor if you expect to return multiple rows of data.
    If you are doing this in a loop, I would strongly suspect that you should be letting Oracle join the tables in SQL rather than doing your own pseudo-join logic in PL/SQL. Letting SQL do the work of joining tables is going to generally be a sustantial performance difference. The difference between the two blocks you posted will be marginal at best.
    Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • How to pass dynamically table name in my cursor declaration

    Hi:
    I am new. could you please let me know how to pass a table name dynamically in my cursor declaration? for instance I am declaring the following cursor in my pl/sql procedure:
    CURSOR crs_validate IS
    select * FROM <ACT_JUN_2006_LOB>;
    this ACT_JUN_2006_LOB table name, I should able to pass it when I open the cursor ... any help appreciated. thanks.
    srini

    Thanks all for the response. REFCURSOR does work: here is an example ... which I found on-line
    procedure emp_test(
    month varchar2,
    year varchar2)
    is
    type cur_typ is ref cursor;
    c cur_typ;
    query_str varchar2(200);
    emp_number number := 7900;
    salary number;
    name varchar2(30);
    Begin
    query_str := 'Select empno, ename, sal from emp_' || month ||'_'||year
    || ' where empno = :id';
    open c for query_str using emp_number;
    loop
    fetch c into emp_number, name, salary;
    exit when c%notfound;
    dbms_output.put_line(emp_number);
    end loop;
    close c;
    end;

  • Package Procedure cursor declaration

    Hi am facing this issues , I am too close the problem to figure. Pl help
    procedure addRox(p_reg_type_id in  number, p_offender_id in  number,p_sentence_end_date in  date,
                                p_registration_date in  date,p_end_registration_date in  date,
                                p_aggravated in  varchar2,p_habitual in  varchar2, p_comments  in varchar2, p_status  in varchar2 DEFAULT null  , p_OFFENSE_CODE in number) is
      cursor tierNum is
        select max(c.tier) from sor_offense o, sor_offense_code c
        where o.offender_id = p_offender_id
        and o.offense_code = c.CODE_ID
        and o.state = 30658
        and upper(o.status) = 'ACTIVE';
      tier number;
      vEndRegDate registration_offender_xref.END_REGISTRATION_DATE%type default null;
    begin
         open tierNum;
         Fetch tierNum into tier;
          if tierNum%NotFound then
            tier := Null;
          end if;
        Close tierNum;
        if tier is not null then
          if(p_sentence_end_date is null) then -- sentenceEndDate is null
              if tier = 1 then
              vEndRegDate := add_months(p_registration_date - 1,180);
              end if;
            if tier = 2 then
              vEndRegDate := add_months(p_registration_date - 1,300);
            end if;
            if tier = 3 then
                vEndRegDate := Null;
            end if;
          else -- sentence_end_date is not null
            if tier = 1 then
              vEndRegDate := add_months(p_sentence_end_date - 1,180);
            end if;
            if tier = 2 then
              vEndRegDate := add_months(p_sentence_end_date - 1,300);
            end if;
            if tier = 3 then
              vEndRegDate := Null;
            end if;
          end if;
        end if;
              insert into registration_offender_xref (reg_type_id, offender_id, status,sentence_end_date,
                                registration_date,end_registration_date,aggravated,habitual,status_date, comments)
                         values (p_reg_type_id, p_offender_id, 'Active',p_sentence_end_date,
                                p_registration_date,vEndRegDate,p_aggravated,p_habitual,sysdate, p_comments);
            -- commit;
    exception
      when others then
            DBMS_OUTPUT.PUT_LINE('ERR in  '||sqlerrm);
    end addRox;\
    error:Error(4,12): PLS-00323: subprogram or cursor 'ADD_ABC' is declared in a package specification and must be defined in the package body

    >
    Hi am facing this issues , I am too close the problem to figure. Pl help
    error:Error(4,12): PLS-00323: subprogram or cursor 'ADD_ABC' is declared in a package specification and must be defined in the package body
    >
    You aren't getting that exception from the code you posted. There is nothing with 'ABC' in any of that code.
    If you want help you need to post the code that you need help with.

  • BUG?: unexpected token in connection pane for cursor declaration

    If I declare a cursor with an order clause in the declaration part of a procedure in a package I will get the unexpected token. The code compiles without errors.
    SQL Developer 15.57.
    Windows XP SP2
    Oracle 10g
    example:
    declare procedure test( param in integer ) as
    cursor cur( nVal in integer ) is
    select * from tab where col1 = nVal
    order by col2;
    begin
    NULL;
    end test;
    If I comment out the order by line and proper end the line before all is OK in connection pane.

    There are a few issues logged for these unexpected tokens. We will be reviewing this section for 1.1.
    Regards
    Sue Harper

  • Column alias declaration in package.

    Hi,
    <br><br>
    OS - XP
    <br><br>
    Version - Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
    <br><br>
    How do I declare a column alias in a cursor?
    <br><br>
    I have the following in a package procedure and get the following error
    <br><br>
    Error(107,35): PLS-00302: component 'EMAIL_FROM' must be declared
    <br><br>
    I have hilighted what I assume is the offending lines, but the error is pointing to the lines in bold italics towards end of code.
    <br><br>
      FOR c2 IN
        (SELECT b.eml_id
              , a.email "email_from"
    , to_address.email "email_to"
              , to_address.subject
              , to_address.header
              , to_address.footer   
           FROM mySchema.table_1 a
          HTMLDB_MAIL.SEND(p_to   => c2.email_from  
                         , p_from => c2.email_to
    ...<br><br>
    Thank You<br>
    Ben
    Message was edited by:
    Benton

    You dont need double quotes " "
    (SELECT b.eml_id
              , a.email email_from
              , to_address.email email_to
              , to_address.subject
              , to_address.header
              , to_address.footer   
           FROM mySchema.table_1 a    
              , mySchema.table_4 b
              , mySchema.table_5 c
              , (SELECT e.eml_id          

  • Passing in values to a cursor in a package procedure

    Hi all
    I have a package :- test
    i have a procedure in the package :- test_proc
    in the procedure i have a cursor
    which has a select statement
    select jobid,jobname from jobs
    where jobcode = p_job_code; -- i think this is wrong
    when i execute the package i pass the job code to the package as a parameter which i use in the cursor above
    as a parameter i have values(i can select any of the above values )
    job code :-
    10
    20
    10,20
    how can i pass the values to the cursor in the procedure
    it is giving me invaiid number;

    Dear abcdxyz!
    As already stated you should try this with dynamic SQL. Here is an example for you:
    CREATE OR REPLACE PROCEDURE job_cursor(p_job_code VARCHAR2)
    IS
      l_jobid      NUMBER(5);
      l_jobname    VARCHAR2(30);
      c_job_cursor INTEGER;
      l_ignore     INTEGER;
    BEGIN
      -- open cursor on source table
      c_job_cursor := DBMS_SQL.Open_Cursor;
      -- parse the SELECT statement
      DBMS_SQL.parse(c_job_cursor, 'SELECT jobid, jobname FROM job WHERE job_code IN (' || p_job_code || ')', DBMS_SQL.NATIVE);
      -- define the column type
      DBMS_SQL.Define_Column(c_job_cursor, 1, l_jobid);
      DBMS_SQL.Define_Column(c_job_cursor, 2, l_jobname, 30);
      ignore := DBMS_SQL.Execute(c_job_cursor);
      LOOP
      -- Fetch a row from the source table
        IF DBMS_SQL.Fetch_Rows(c_job_cursor) > 0 THEN
            -- get column values of the row
            DBMS_SQL.Column_Value(c_job_cursor, 1, l_jobid);
            DBMS_SQL.Column_Value(c_job_cursor, 2, l_jobname);
        ELSE
           -- No more rows to copy
          EXIT;
        END IF;
      END LOOP;
      DBMS_OUTPUT.PUT_LINE(l_jobid || ', ' || l_jobname);
      DBMS_SQL.Close_Cursor(c_job_cursor);
    EXCEPTION
      WHEN OTHERS THEN
        IF DBMS_SQL.Is_Open(c_job_cursor) THEN
          DBMS_SQL.Close_Cursor(c_job_cursor);
        END IF;
        RAISE;
    END;
    /Yours sincerely
    Florian W.
    P.S. I haven't tested this procedure.

  • How to divide resultset of a query in different ref cursors in a package

    Hi Oracle Gurus,
    I need to create a package which counts the no of rows returned by a select query on multiple tables and according to the returned rowcount inputs the resultset in a ref cursor. Procedure will be called by a .NET program and output param refcursor will be assigned to a data reader which will redirect all the data to an Excel file.
    All the above is done. Issue is due to Excel's limit of 64000 rows, if data returned by query is greater than 64K it wont be fit in 1 Excel sheet. So, in order to overcome this limit I need to do some looping in Oracle package which keeps on storing the query results (rows<64K) in different ref cursors so that these refcursors as OUT params can be redirected to separate Excel sheets in C# program.
    NOTE : Earlier on I created 2 procedures in the package to fetch rows<64K and another one to fetch rows between 64K and rowcount of the query. My program was calling 2 different procedures to redirect data into 2 diff Excel sheets.
    But this fails when query resultset is even greater than 128000 or more and demands 3-4 or even more Excel sheets to be created.
    Please help.
    Any idea how to do looping in Oracle to accomplish this?

    > So, in order to overcome this limit I need to do some looping in Oracle package which keeps on
    storing the query results (rows<64K) in different ref cursors so that these refcursors as OUT params
    can be redirected to separate Excel sheets in C# program.
    Huh?
    That is saying that "I need to change this road and build it straight through that lake as the road has a curve here".
    It surely is a LOT easier to leave the road as is and simply turn the darn steering wheel in the car?
    Have the .Net data reader keep a row count of rows read from the ref cursor - and when it reached a pre-set max, have the reader do a "control break"[1] and change to a new worksheet as the destination for writing the rows to.
    [1] Google the term if you do not understand this basic concept that was among the very basic program control structures taught back in the 80's.. while I foam at the mouth how today's "wonder kids" turned programmers, that grew up with computers, do not even comprehend the most basic programming logic designs...

  • Same variable declaration in package

    Hi
    Please go through below lines of code where I have declared same variable 2 times, which got compiled successfully. But If I do the same in stand alone procedure it throws an error. In case of creation of package it says package created. Can anyone justify to this .....
    SQL> create or replace package
    2 may221
    3 as
    4 a number := 10 ;
    5 a number := 20 ;
    6 procedure p1(a number) ;
    7 end ;
    8 /
    Package created.
    SQL> create or replace package body may221
    2 as
    3 procedure p1(a number)
    4 as
    5 begin
    6 dbms_output.put_line(a) ;
    7 end ;
    8 end ;
    9 /
    Package body created.
    SQL> set serverout on
    SQL> exec may221.p1(20);
    20
    PL/SQL procedure successfully completed.
    SQL> begin
    2 dbms_output.put_line(may221.a) ;
    3 end ;
    4 /
    dbms_output.put_line(may221.a) ;
    ERROR at line 2:
    ORA-06550: line 2, column 32:
    PLS-00371: at most one declaration for 'MAY221.A' is permitted in the declaration section
    ORA-06550: line 2, column 4:
    PL/SQL: Statement ignored
    Thanks and Regards
    JC

    > I checked it's working fine.. no errors..
    Oracle version?
    There is an error as there are two definitions of variable a. This will generate a run-time error when that variable is used.
    However, the result of procedure P1 is not an error.
    It is all a matter of scope. Scope determines the resolution of a reference (like a variable name, object name, column name, table name, etc.)
    The scope is first local - which means the local code block is checked for resolution. And the variable a is found. In o-o terms, when you refer to variable a it is first resolved as self.a - and only when that fails, the scope changes to the "encapsulating" unit which is the package.
    The following code illustrates:
    SQL> create or replace package FooPackage is
    2 a number := 10;
    3 a number := 11;
    4
    5 procedure Show( a number );
    6 end;
    7 /
    Package created.
    SQL> show errors
    No errors.
    SQL>
    SQL>
    SQL> create or replace package body FooPackage is
    2 procedure Show( a number ) is
    3 begin
    4 DBMS_OUTPUT.put_line( a ); -- <= resolved as self.a
    5 end;
    6 end;
    7 /
    Package body created.
    SQL> show errors
    No errors.
    SQL>
    SQL>
    SQL> exec FooPackage.Show( 12 )
    12
    PL/SQL procedure successfully completed.
    SQL> exec DBMS_OUTPUT.put_line( FooPackage.a );
    BEGIN DBMS_OUTPUT.put_line( FooPackage.a ); END;
    ERROR at line 1:
    ORA-06550: line 1, column 40:
    PLS-00371: at most one declaration for 'FOOPACKAGE.A' is permitted
    ORA-06550: line 1, column 7:
    PL/SQL: Statement ignored

  • Cursor Declaration

    I am trying to declare a cursor in a stored procedure using the following sql:
         CURSOR conversionCursor is select customer_number, company_id , buyer_id
         from hpsiuser.conversion_master natural join
         (select distinct mfg_code, customer_number
                                                 from hpsiuser.vendor_info, invoice_h
                                                 where vendor_id = vendorID);
    I get the following errors:
    Line # = 6 Column # = 29 Error Text = PL/SQL: SQL Statement ignored
    Line # = 6 Column # = 9 Error Text = PLS-00341: declaration of cursor 'CONVERSIONCURSOR' is incomplete or malformed
    Line # = 7 Column # = 43 Error Text = PL/SQL: ORA-06552: PL/SQL: Compilation unit analysis terminated ORA-06553: PLS-320: the declaration of the type of this expression is incomplete or malformed
    My assumption is that it is having a problem with the join yet it works as a stand alone select statement.
    Any insight would be appreciated.
    Jon King

    CURSOR Expressions
    A CURSOR expression returns a nested cursor. This form of expression is equivalent to the PL/SQL REF CURSOR and can be passed as a REF CURSOR argument to a function.
    cursor_expression::=
    Text description of cursor_expression
    A nested cursor is implicitly opened when the cursor expression is evaluated. For example, if the cursor expression appears in a SELECT list, a nested cursor will be opened for each row fetched by the query. The nested cursor is closed only when:
    The nested cursor is explicitly closed by the user
    The parent cursor is reexecuted
    The parent cursor is closed
    The parent cursor is cancelled
    An error arises during fetch on one of its parent cursors (it is closed as part of the clean-up)
    Restrictions on CURSOR Expressions
    If the enclosing statement is not a SELECT statement, nested cursors can appear only as REF CURSOR arguments of a procedure.
    If the enclosing statement is a SELECT statement, nested cursors can also appear in the outermost SELECT list of the query specification, or in the outermost SELECT list of another nested cursor.
    Nested cursors cannot appear in views.
    You cannot perform BIND and EXECUTE operations on nested cursors.
    http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/expressions6a.htm#1035109
    Joel P�rez

  • Report based on Ref Cursor and lock package/table (ORA-04021)

    Hi,
    I have reports based on Ref Cursor. Report builder and Reports Background Engine make pins for package, which consist of Ref Cursor, and for tables, which is used in package. So I can't grant any privileges on these tables anybody.
    For example, after next statement:
    GRANT SELECT ON table_name TO user_name
    I received the next error:
    ORA-04021: timeout occurred while waiting to lock object table_name
    Thanks

    Hi,
    I have reports based on Ref Cursor. Report builder and Reports Background Engine make pins for package, which consist of Ref Cursor, and for tables, which is used in package. So I can't grant any privileges on these tables anybody.
    For example, after next statement:
    GRANT SELECT ON table_name TO user_name
    I received the next error:
    ORA-04021: timeout occurred while waiting to lock object table_name
    Thanks

  • How to create an VO (EO ?) from a cursor got from package ?

    Hello,
    I have a packaged function that returns a "ref cursor" object.
    How can I use this to create a VO (or an EO, but I doubt about this) ?

    Thanks for the link, but it doesn't answer the question :(
    I have a packaged function that returns a cursor :
    mypackage.get_my_cursor(param1, param2)
    Is is possible to create an EO / VO based on the return of this function ?

Maybe you are looking for

  • HELP!! Hyperlinks in the wrong place

    Please Help!!! I have created a a word document with Bookmarks and hyperlinks to the bookmarks and it works fine however when i upload it to adobe proffesional some of the links go the wrong pages??? This is all new to me but i cant work out whats go

  • How do i track a stolen ipod touch

    how do i track a stolen ipod touch

  • Unit Of Measue issue in Alternative Unit Of Measure

    Dear All, We have one issue regarding material volume which is having Base Unit Of Measure as 'L' - Litre. Example: one material AAA is having BUoM as 'L' but this material volume is .002 M3. This value we can maintain in Basic Data but we are unable

  • PS elements 6 (folder view)

    In PS elemenents 5 I was able to view pictures by folder name. I was unable to do the same in PS elements 6. Is it possible to organize/view pictures by folder name in PS elements 6? If yes, how? Thanks baktoo

  • I want to open in new window

    I hate tabs, I want to "open in a new window". I used to be able to turn off tabs in the setting but that option is gone.