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

Similar Messages

  • 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

  • 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

  • Evaluation of expressions in cursor declaration.

    Hi, all,
    If I have in an on-demand application process:
    DECLARE
    CURSOR c1 IS SELECT DUMMY FROM DUAL WHERE 'FOO' = V('MY_ITEM')
    BEGIN
    FOR c IN c1
    LOOP
    -- do stuff with c
    END LOOP;
    END
    when is V('MY_ITEM') evaluated?

    V('MY_ITEM') will be evaluated when the cursor is opened on (every) execution of the on-demand process.
    Toon

  • Xmltable in cursor declaration - error!

    Hi, I am using the following SQL:
    select l.description
    from XMLTABLE(XMLNAMESPACES('PostcodeAnywhere2' as "e"), '/e:InterimResults/e:Results/e:InterimResult' PASSING
    XMLTYPE.createxml(ws_postcode_aw.getByPostcodeXML('WR2 6NJ','1', 'XML'))
    COLUMNS description varchar2(400) PATH '//e:Description'
    ) l
    which returns data OK.
    However, when I incorporate it into a cursor, I get a "no more data to read from socket" error. When I replace the cursor with a simple "SELECT 'dasda' description FROM dual" the code complies OK. Can anyone assist with this? I have no idea what could be causing this. Code is included below:
    CREATE OR REPLACE
    PACKAGE BODY "GET_PC_DATA" AS
    PROCEDURE disp_pc_data IS
    v_str clob;
    v_post_code varchar2(10) := 'WR2 6NJ';
    v_fast_add_ret_xml xmltype;
    v_pc_lookup_ret_xml xmltype;
    TYPE c_get_addresses_t IS REF CURSOR;
    c_get_addresses c_get_addresses_t;
    TYPE r_get_addresses_t IS RECORD ( description varchar2(400));
    r_get_addresses r_get_addresses_t;
    FUNCTION getHouseNumber(p_desc IN varchar2) RETURN varchar2
    IS
    BEGIN
    return substr(p_desc,1,instr(p_desc,v_1_line_add));
    END;
    BEGIN
    OPEN c_get_addresses FOR
    SELECT l.description
    FROM XMLTABLE(XMLNAMESPACES('PostcodeAnywhere2' as "e"), '/e:InterimResults/e:Results/e:InterimResult' PASSING
    XMLTYPE.createxml(ws_postcode_aw.getByPostcodeXML('WR2 6NJ','1', 'XML'))
    COLUMNS description varchar2(400) PATH '//e:Description'
    ) l;
    LOOP
    FETCH c_get_addresses INTO r_get_addresses;
    EXIT WHEN c_get_addresses%NOTFOUND;
    v_str := v_str || getHouseNumber(r_get_addresses.description);
    END LOOP;
    CLOSE c_get_addresses;
    END;
    END;
    Message was edited by:
    steve_macleod

    V('MY_ITEM') will be evaluated when the cursor is opened on (every) execution of the on-demand process.
    Toon

  • Declaring private cursors in package header

    Hi.
    Im tunning a PLSQL package , and im having the following doubt.
    This package has alot of cursors declared in the package header, but the cursors are private. (called within the package only)
    Could the public functions from the package have a loss of performance by using those cursors? (should the private cursors be declared within the functions or at least inside package body?)
    Im kind of new with plsql, so please bare with me :) .
    Cheers.

    there's no performance loss.
    however, unless the cursors are to be accessible outside the package (which is what happens when they are declared in the spec (not header)), then there's no benefit either. if they are truely meant to be private, then you can remove them from the spec. and if you're wrong, and they are called from elsewhere, you'll find out soon enough.

  • Problems while using ORDER BY in declaring a PL/SQL cursor

    Hi, everybody
    I hope you can help me.
    We are programming a very simple PL/SQL Procedure where we are declaring the following Cursor:Declare
    Cursor CUnidadP is
    Select UNOR_CO_UNID_ORGID,UNOR_CO_PADREID,UNOR_NU_NIVEL from Unidad_Organizativa
    where Unor_NU_NIVEL > 1)
    order by Unor_NU_NIVEL;
    (We need to have sorted records in the cursor)
    Curiously, this cursor declaration works fine in our local 8.1.6 Database (in an NT machine) but it is not working in our customer Database (8.0.4.3.0 for SUN/SOLARIS)
    where we are getting the following error messages:
    "ORA-06550:line 3, column 9
    PLS-00707:unsupported construct or internal error (2603)
    ORA-06550:line 3, column 9
    PL/SQL:SQL Statement ignored"
    So, my question is: Anyone knows why? Is there any bug concerning to 8.0.4.3.0 version for SUN Solaris referring to this subject? Does anyone know a different way of doing this getting same results?
    Thanks everybody and best regards
    Irene

    There is a bug with the following description:
    Bug:619477
    Description: Although SQL supports the ORDER BY clause in subqueries,
    PL/SQL does not yet support these.
    Workaround: Execute SQL statements with such subqueries through dynamic SQL.
    An easier workaround is an explicit (out-of-line) view
    Maybe this (or a related bug) is your problem

  • Declaring a cursor

    hi all,
    is it possible to declare a cursor like this ..???
    declare
    cursor is select ab.col1,cd.col2 from
    (select a.col1,b.col2
    from a,b
    conditions)ab
    (select c.col1,d.col2...
    from c,d
    conditions..)cd
    i am getting the following error
    PL/SQL: ORA-00923: FROM keyword not found where expected

    Plenty of syntactical error in your cursor declaration. It should be like this ->
    cursor c1
    is
      select ab.col1,
             cd.col2
      from (
             select a.col1,
                    b.col2
             from a,b
             where conditions
           ) ab,
            select c.col1,
                   d.col2...
            from c,d
            where conditions..) cd
      where ab.cond1 = cd.cond1;Regards.
    Satyaki De.

  • T-SQL: Cursor is not advancing to next record

    SQL Version:  2008 R2
    Problem:  I have the code below which uses a cursor.  The cursor keeps returning the first record and does not advance to the next record in the cursor.  It appears as if the Fetch Next is not being recognized.  The Select
    Statement in the cursor declaration returns two records which is the result set I expect to be contained in the cursor record set.
    ...bob sutor
    SQL CODE:
    DECLARE
      @ProcessGroupID nchar(4)
     , @RemoveAuditUser nchar(128)
    DECLARE CertGroupCursor CURSOR FOR
     SELECT DISTINCT CertGroups.GroupCode, CertGroups.RemoveAuditUser
         --, UserControl.ProcessGroupID, UserControl.VPUserName
     FROM udCertGroups AS CertGroups
      LEFT JOIN udAuditUsers AS UserControl
      ON CertGroups.GroupCode = UserControl.ProcessGroupID
     WHERE CertGroups.GroupCode = UserControl.ProcessGroupID
      AND CertGroups.RemoveAuditUser = UserControl.VPUserName
    OPEN CertGroupCursor
     FETCH NEXT FROM CertGroupCursor INTO @ProcessGroupID, @RemoveAuditUser
     WHILE @@FETCH_STATUS = 0
     Print @ProcessGroupID + '-' + @RemoveAuditUser
     DELETE FROM udAuditUsers
      WHERE ProcessGroupID = @ProcessGroupID
       AND VPUserName = @RemoveAuditUser
     FETCH NEXT FROM CertGroupCursor INTO @ProcessGroupID, @RemoveAuditUser
    CLOSE CertGroupCursor
    DEALLOCATE CertGroupCursor
    Bob Sutor

    The real question is how to get rid of this mess. Think about the local “@remove_audit_user” as a variable; it's name is a verb, not a noun! and the NVARCHAR(n) lets you use Chinese Unicode. Why? In ISO-11179 rules , “remove_” is a called a role, and the
    audit user would be the attribute with the attribute property “_user” in a valid data model. Where is the table that models “audit_users”? It has to be here by definition. 
    One of the first rules of data modeling is that a data element has one and only one name. This is a results of the Law of Identity in Logic (A is A: to be is to be something in particular, to be nothing in particular or many things in general is to be nothing
    at all). 
    So how did “G.remove_audit_user = U.vp_user_name” occur??  ANSWER: lack of a design!
    Your “G.group_code = U.process_group_id” is wrong. An identifier is not a code! TOTALLY DIFFERENT type of data elements! Do you have a postal code or a postal id? Blood_type or blood_id?  Etc.? Have you ever read a book on basic data modeling? 
    The purpose of PRINT is debugging and not output. We had  joke in the ANSI X3H2 Committee that SQL means “scarcely Qualified as a Language” because there is no I/O. PRINT will screw up performance in so many ways. 
    In a properly designed schema, we seldom use SELECT DISTINCT; we have keys and a valid schema that does not produce redundant duplicate rows. It might be valid, but after 30+ years of SQL, I would bet against it. 
    Your statement would use an EXISTS() predicate to handle multiple columns and conditions. But you did not bother with DDL, as required by basic Netiquette, so here is the skeleton I can give you. 
    DELETE FROM UD_Audit_Users
     WHERE EXIST
           (SELECT *
              FROM UD_Cert_Groups AS G
             WHERE G.process_group_id = ?? 
               AND G.vp_user_name = ??;
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • INVALID CURSOR - Anonymous Block calling Cursor in function

    I am getting an error when trying to call my cursor.
    CREATE OR REPLACE PACKAGE tax_update
    AS
    TYPE gencur IS ref cursor;
    FUNCTION tax_sf
       p_state IN bb_tax.state%type,
       p_thecursor IN OUT gencur
    RETURN NUMBER;
    END;
    CREATE OR REPLACE PACKAGE BODY tax_update
    AS
    FUNCTION tax_sf
       p_state IN bb_tax.state%type,
       p_thecursor IN OUT gencur
    RETURN NUMBER
      IS
      lv_taxrate NUMBER;
    BEGIN
      OPEN p_thecursor FOR
       SELECT taxrate
       FROM bb_tax
       WHERE state = p_state;
      RETURN lv_taxrate;
    END;
    END;
    DECLARE
      tax_cur tax_update.gencur;
      rec_tax bb_tax%rowtype;
    BEGIN
    LOOP
      FETCH tax_cur INTO rec_tax;
       EXIT WHEN tax_cur%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(rec_tax.taxrate);
    END LOOP;
    END;
    DECLARE
    ERROR at line 1:
    ORA-01001: invalid cursor
    ORA-06512: at line 6Assignment is to create a package that will hold tax rates by state in a packaged cursor. The package will contain a function that can receive a 2 character state abbr. as an argument and find a match in the cursor and return the tax rate for tha tstate. An anonymous block will test the function with state of NC.
    Can anyone assist?

    You would need to call the function to open the cursor before you try to fetch from the cursor
    DECLARE
      tax_cur tax_update.gencur;
      rec_tax bb_tax%rowtype;
      l_some_number number;
    BEGIN
      l_some_number :=  tax_update.tax_sf( <<some state parameter>>, tax_cur );
      LOOP
        FETCH tax_cur INTO rec_tax;
        EXIT WHEN tax_cur%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(rec_tax.taxrate);
      END LOOP;
    END;A couple of points, though.
    1) Your function returns a NUMBER but that NUMBER will always be NULL. It seems rather unlikely that this is really what you want. It would seem to make more sense for the function to return the cursor rather than returning a superfluous number.
    2) Your function requires a `bb_tax.state%type` parameter. But your anonymous block doesn't seem to have any concept of a state so I'm not sure what you want to pass in there.
    3) Looking at the code, it seems a bit odd that your cursor returns a single column of data. If a state can have multiple rates, wouldn't you need to select some additional criteria in order to figure out which sort of tax each row represents or to otherwise differentiate different rows? If a state can only have a single tax rate, it makes no sense to open a cursor that is only going to ever return a single row.
    4) There is no need to declare your own weak ref cursor type (tax_update.gencur). You can just use the Oracle built-in type SYS_REFCURSOR.
    Justin

  • Loading data from one table to another using cursor

    Hi,
    I have given the below command to load the data from 1 table to another using cursor.
    declare
    cursor mycursor IS
    SELECT extract_name,from_date,to_date,BETA from temp_table where EXTRACT_NAME='GIFTCARD_DETAILS';
    Begin
    for mycursor_1 IN mycursor loop
    insert into tmp_tab columns(col1,col2,col3,col5) values(mycursor_1.EXTRACT_NAME,mycursor_1.from_date,mycursor_1.to_date,mycursor_1.BETA);
    End loop;
    commit;
    end;
    It is working fine.
    But I want to hard code some of the columns ( like flags ) which are not there in 1st table and load them into 2nd table.
    In db2 we will give commands like
    varSqlStatus=`db2 "declare mycurs cursor for select extract_name,from_date,to_date,BETA,'N','Y' from temp_table"`
    varSqlStatus=`db2 "load from mycurs of cursor modified by identityignore insert into tmp_tab(col1,col2,col3,col5,col6,col7) nonrecoverable"`
    But I want it in oracle 10g, Can any one help me in this.

    Have you tried either of the two options :
    1. Modify the CURSOR itself :
    cursor mycursor IS
    SELECT extract_name,from_date,to_date,BETA,'N','Y' from temp_table where EXTRACT_NAME='GIFTCARD_DETAILS';2. Modify the INSERT statement itself :
    insert into tmp_tab columns(col1,col2,col3,col5,col6,col7) values(mycursor_1.EXTRACT_NAME,mycursor_1.from_date,mycursor_1.to_date,mycursor_1.BETA,'N','Y');

  • Getting error which passing a variable to the cursor

    hi all,
    i am passing pl/sql table value into the sql cursor
    DECLARE
      CURSOR cr(i_oList IN tList) IS
        SELECT SOMETHING
          FROM SOMETABLE
         WHERE SOMECOL IN (SELECT COLUMN_VALUE FROM TABLE(CAST(i_oList AS tList));but i am getting following error :invalid variable declaration 'tList' must be type or subtype.
    please help me

    HI,
    i changed my code to
    DECLARE
    CURSOR cr(i_oList tList) IS
    SELECT SOMETHING
    FROM SOMETABLE
    WHERE SOMECOL IN (SELECT COLUMN_VALUE FROM TABLE(tList));
    still getting the same error as i mentioned before
    "invalid variable declaration: object tlist must be type or subtype                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Maybe you are looking for

  • Problems with my Audigy Ga

    OS: Windows 2000 Motherboard: A-bit IC7-G Sound Card: Audigy Gamer Video Card: EVGA Geforce 6800 ULTRA RAM: 2gig My problem is a weird one, my sound card would be working fine, the sound was great and everything was peachy. However, the moment I star

  • Regarding video performance.

    Hi, I bought a MSI P7N SLI Platinum 750i chipset based motherboard Sn. 010B0807485693.  After 8 months the board would not post, i.e. no display on the monitor even though the Harddisk LED and the CPU fans would run. Handed over the motherboard to Mi

  • How do I retrieve the answers to my security questions?  I did not write them down.

    How do I retrieve the answers to my security questions?   I did not write them down or I did but have lost them. Thank you

  • Creative Cloud for student

    Can we install CC with a student license on a desktop and a laptop computer at the same time if owned by the same person?? If yes, tell me how, the program ask me to activate the programs on the second computer every time I open an application

  • Copy iPhone music library into iTunes library?

    I accidentaly deleted my entire music library, is there a way to copy my music off my iPhone into my iTunes library?