Loop cursor in plsql

Hi,
I create a procedure which is something like below:
create or replace procedure INSERT_DATA is
cursor cursor1(vfield varchar2) is
select field1,field2,field3,field4
from tableb z, xmltable( .....) x
       where z.field1=vfield
begin
  for c in (select fieldname from tablea)
  loop
    for t in cursor1(c.fieldname)
    loop
     insert into tablec
      (field1,field2,field3,field4)
      values
      (t.field1,t.field2,t.field3,t.field4);
    end loop; 
    commit;
  end loop;
end INSERT_DATA;when I run this procedure, I found that the memory usage of Oracle process in the server will keep increasing,
I want to run this procedure against some huge table, if the memory usage keep increase, I am afraid that it will exhaust the memory.
Is there any way to improve this?
Thanks
Vincent
Edited by: pj**** on 15-Sep-2011 00:42

Hi, Odie
Thanks for remember me, the actual query is as below
select testname,StepName,Status,Comp,Data,TESTSTATUS,SingleTest
from cust_vw_test_log_clob z, xmltable(
       declare function local:getChildren($p as element()) as element()*
         for $i in $p
         return
         if ($i/Prop[5]/@Name="Measurement")
         then (
             for $t in $i/Prop[@Name="Measurement"]/Value
             return
             element r {
                     element StepName1 {$t/Prop/@Name}
                    ,element Status1 {$t/Prop/Prop[@Name="Status"]/Value/text()}
                    ,element Comp1 {$t/Prop/Prop[@Name="Comp"]/Value/text()}
                    ,element Data1 {$t/Prop/Prop[@Name="Data"]/Value/text()}
                    ,element Testname1 {$i/Prop[@Name="TS"]/Prop[@Name="StepName"]/Value/text()}
                    ,element TEST_STATUS {$i/Prop[@Name="Status"]/Value/text()}
         else
             for $t in $i
           return
           element r {
                 element SingleTest {$t/Prop[@Name="TS"]/Prop[@Name="StepName"]/Value/text()}
                ,element TEST_STATUS {$t/Prop[@Name="Status"]/Value/text()}
                ,element Data1 {$t/Prop[@Name="Numeric"]/Value/text()}
             for $a in $i/Prop[@Name="TS"]/Prop[@Name="SequenceCall"]/Prop[@Name="ResultList"]/Value
             return
             local:getChildren($a/Prop[@Type="TEResult"])
        local:getChildren(/Reports/Report/Prop[@Type="TEResult"])'
       passing xmltype(z.test_log_text)
       columns
       StepName varchar2(500) path 'StepName1/@Name',
       Status varchar2(500) path 'Status1',
       Comp varchar2(500) path 'Comp1',
       Data varchar2(500) path 'Data1',
       TestName varchar2(100) path 'Testname1',      
       TestStatus varchar2(100) path 'TEST_STATUS',
       SingleTest varchar2(100) path 'SingleTest'      
      ) x
       where z.ticket=vTicketvincent

Similar Messages

  • FOR LOOP cursor that updates table A based on a value in table B

    Hi,
    I need a FOR LOOP cursor that scans and updates all pro-rata column in table EMPLOYEE(child) based on what pay classification all employees are on in the CLASSIFICATION(parent) table.
    DECLARE
    BEGIN
    IF employee.emp_type = 'FT' THEN
    UPDATE employee
    SET employee.pro_rata = ((classification.yearly_pay/52)*52)
    WHERE employee.empid = v_empid AND classification.class_id = employee.class_id;
    END IF;
    IF employee.emp_type = 'PT1' THEN
    UPDATE employee
    SET employee.pro_rata = ((classification.yearly_pay/39)*52)
    WHERE employee.empid = v_empid AND classification.class_id = employee.class_id;
    END IF;
    IF employee.emp_type = 'PT2' THEN
    UPDATE employee
    SET employee.pro_rata = ((classification.yearly_pay/21)*52)
    WHERE employee.empid = v_empid AND classification.class_id = employee.class_id;
    END IF;
    END;
    How do I create a cursor that cuts across these two table
    See tables and data
    CREATE TABLE CLASSIFICATION(
    CLASS_ID VARCHAR2(6) NOT NULL,
    CLASS_TYPE VARCHAR2(10),
    DESCRIPTION VARCHAR2(30) NOT NULL,
    YEARLY_PAY NUMBER(8),
    HOURLY_RATE NUMBER,
    WEEKDAY_OVER_TIME NUMBER,
    WEEKEND_OVER_TIME NUMBER,
    CONSTRAINT PK_CLASS_ID PRIMARY KEY (CLASS_ID));
    INSERT INTO CLASSIFICATION VALUES('PR1','PERMANENT','MANAGER',45000,'','',NULL);
    INSERT INTO CLASSIFICATION VALUES('PR2','PERMANENT','ADMIN ASSISTANT',22000,'',1.5,NULL);
    INSERT INTO CLASSIFICATION VALUES('PR3','PERMANENT','CONTROLLER',32000,'',1.5,NULL);
    INSERT INTO CLASSIFICATION VALUES('PR4','PERMANENT','CASH OFFICER',22000,'',1.5,NULL);
    INSERT INTO CLASSIFICATION VALUES('PR5','PERMANENT','CLEANERS',16000,'',1.5,NULL);
    INSERT INTO CLASSIFICATION VALUES('PR6','PERMANENT','ADMIN OFFICER',22000,'',1.5,NULL);
    INSERT INTO CLASSIFICATION VALUES('PR7','PERMANENT','WAREHOUSE ATTENDANT',20000,'',1.5,NULL);
    INSERT INTO CLASSIFICATION VALUES('PR8','PERMANENT','WINDOWS DRESSER',22000,'',1.5,NULL);
    INSERT INTO CLASSIFICATION VALUES('PR9','PERMANENT','DIRECTOR',60000,'','',NULL);
    INSERT INTO CLASSIFICATION VALUES('PR10','PERMANENT','DEPUTY DIRECTOR',52000,'','',NULL);
    INSERT INTO CLASSIFICATION VALUES('PR11','PERMANENT','SALES ASSISTANT',21000,'',1.5,NULL);
    INSERT INTO CLASSIFICATION VALUES('TEMP2','TEMP STAFF','ADMIN ASSISTANT','',16.50,'',NULL);
    INSERT INTO CLASSIFICATION VALUES('TEMP3','TEMP STAFF','CONTROLLER','',29.00,'',NULL);
    INSERT INTO CLASSIFICATION VALUES('TEMP4','TEMP STAFF','CASH OFFICER','',19.00,'',NULL);
    INSERT INTO CLASSIFICATION VALUES('TEMP5','TEMP STAFF','CLEANERS','',10.00,'',NULL);
    INSERT INTO CLASSIFICATION VALUES('TEMP6','TEMP STAFF','ADMIN OFFICER','',20.00,'',NULL);
    INSERT INTO CLASSIFICATION VALUES('TEMP7','TEMP STAFF','WAREHOUSE ATTENDANT','',18.00,'',NULL);
    INSERT INTO CLASSIFICATION VALUES('TEMP8','TEMP STAFF','WINDOWS DRESSER','',18.50,'',NULL);
    INSERT INTO CLASSIFICATION VALUES('TEMP11','TEMP STAFF','SALES ASSISTANT','',16.00,'',NULL);
    CREATE TABLE EMPLOYEE(
    EMPID NUMBER(5) NOT NULL,
    SURNAME VARCHAR2(30) NOT NULL,
    FNAME VARCHAR2(30) NOT NULL,
    GENDER CHAR(1) NOT NULL,
    DOB DATE NOT NULL,
    EMP_TYPE VARCHAR2(20) NOT NULL,
    ANNUAL_WEEKS_REQD NUMBER(2),
    PRO_RATA_WAGES NUMBER(7,2),
    HOLIDAY_ENTLMENT NUMBER(2),
    SICK_LEAVE_ENTLMENT NUMBER(2),
    HIRE_DATE DATE NOT NULL,
    END_DATE DATE,
    ACCNO NUMBER(8) NOT NULL,
    BANKNAME VARCHAR2(20) NOT NULL,
    BRANCH VARCHAR2(20) NOT NULL,
    ACCOUNTNAME VARCHAR2(20),
    CLASS_ID VARCHAR2(6),
    CONSTRAINT CK_HIRE_END CHECK (HIRE_DATE < END_DATE),
    CONSTRAINT CK_HIRE_DOB CHECK (HIRE_DATE >= ADD_MONTHS(DOB, 12 * 18)),
    CONSTRAINT CK_EMP_TYPE CHECK (EMP_TYPE IN ('FT','PT1','PT2','PT3','HOURLY')),
    CONSTRAINT CK_EMP_GENDER CHECK (GENDER IN ('M','F')),
    CONSTRAINT FK_EMP_CLASS FOREIGN KEY (CLASS_ID) REFERENCES CLASSIFICATION(CLASS_ID),
    CONSTRAINT PK_EMP PRIMARY KEY (EMPID));
    CREATE SEQUENCE SEQ_EMPID START WITH 1;
    INSERT INTO EMPLOYEE VALUES(
    SEQ_EMPID.NEXTVAL,'RICHARD','BRANDON','M','25-DEC-1966','FT',52,22000.00,28,14,'10-JAN-2005',NULL,90823227,'NATWEST','BROMLEY','DEPOSIT','PR2');
    INSERT INTO EMPLOYEE VALUES(
    SEQ_EMPID.NEXTVAL,'BOYCE','CODD','M','15-JAN-1972','PT1','','','','','12-JAN-2005',NULL,72444091,'LLOYDS','KENT','CURRENT','PR8');
    INSERT INTO EMPLOYEE VALUES(
    SEQ_EMPID.NEXTVAL,'ALHAJA','BROWN','F','20-MAY-1970','HOURLY','','','','','21-JUN-2000',NULL,09081900,'ABBEY','ESSEX','CURRENT','TEMP2');
    INSERT INTO EMPLOYEE VALUES(
    SEQ_EMPID.NEXTVAL,'RON','ATKINSON','M','10-AUG-1955','PT3','','','','','12-JAN-2005','26-MAR-2006',01009921,'HALIFAX','KENT','SAVINGS','PR6');
    INSERT INTO EMPLOYEE VALUES(
    SEQ_EMPID.NEXTVAL,'CHAMPI','KANE','F','01-JAN-1965','PT2','','','','','12-JAN-2004',NULL,98120989,'HSBC','ILFORD','CURRENT','PR4');
    INSERT INTO EMPLOYEE VALUES(
    SEQ_EMPID.NEXTVAL,'NED','VED','M','15-JAN-1980','HOURLY','','','','','29-DEC-2005',NULL,90812300,'WOOLWICH','LEWISHAM','CURRENT','TEMP6');
    INSERT INTO EMPLOYEE VALUES(
    SEQ_EMPID.NEXTVAL,'JILL','SANDER','F','22-MAR-1971','FT','','','','','30-NOV-2003',NULL,23230099,'BARCLAYS','PENGE','DEPOSIT','PR1');
    Any contribution would be appreciated
    many thanks
    Cube60

    Hi,
    I have triede this cursor procedure but I get an compilation error.
    See first post for tables and data..
    Can someone help me out please.
    SQL> CREATE OR REPLACE PROCEDURE update_employee(
    2 p_empid employee.empid%type,
    3 p_emp_type employee.emp_type%type)
    4 IS
    5 CURSOR c1 is
    6 select e.empid, e.emp_type, c.yearly_pay from employee e, classification c where
    7 c.class_id = e.class_id;
    8 BEGIN
    9 OPEN c1
    10 LOOP
    11 FETCH c1 INTO p_empid, p_emp_type;
    12 exit when c1%notfound;
    13
    14 IF v_emp_type ='PT1' THEN
    15 UPDATE employee SET annual_weeks_reqd = 39, pro_rata_wages = ((v_yearly_pay/52)*39), holiday_en
    tlment=21, sick_leave_entlment = 10.5 WHERE c.class_id = e.class_id;
    16 END IF;
    17 END;
    18 /
    Warning: Procedure created with compilation errors.
    SQL> SHOW ERR;
    Errors for PROCEDURE UPDATE_EMPLOYEE:
    LINE/COL ERROR
    10/1 PLS-00103: Encountered the symbol "LOOP" when expecting one of
    the following:
    . ( % ; for
    The symbol "; was inserted before "LOOP" to continue.
    Many thanks

  • How to get last Record ior Total rows in For Loop Cursor ?

    Hi Friends
    I would like to know , the last record in for loop cursor, i have the code in following format
    cursor c1 is
    select * from emp;
    begin
    for r1 in c1 loop
    v_total_rec := ? ( i would like to know total rows in the cursor , say for example if cursor has 10 rows, i want10 into this variable )
    v_count := v_count +1;
    dbms_output.put_line(r1.emp_name);
    end loop;
    end;
    Hope i am clear
    Any suggestions?
    Thanks
    Ravi

    Even though cursor loops are generally a Bad Idea ^tm^ as Dan says, here's an example of how you can get the information you wanted within the query itself...
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2    cursor c1 is
      3      select emp.*
      4            ,count(*) over (order by empno) as cnt
      5            ,count(*) over () as total_cnt
      6      from emp
      7      order by empno;
      8  begin
      9    for r1 in c1 loop
    10      dbms_output.put_line(r1.ename||' - row: '||r1.cnt||' of '||r1.total_cnt);
    11    end loop;
    12* end;
    SQL> /
    SMITH - row: 1 of 14
    ALLEN - row: 2 of 14
    WARD - row: 3 of 14
    JONES - row: 4 of 14
    MARTIN - row: 5 of 14
    BLAKE - row: 6 of 14
    CLARK - row: 7 of 14
    SCOTT - row: 8 of 14
    KING - row: 9 of 14
    TURNER - row: 10 of 14
    ADAMS - row: 11 of 14
    JAMES - row: 12 of 14
    FORD - row: 13 of 14
    MILLER - row: 14 of 14
    PL/SQL procedure successfully completed.
    SQL>

  • Process having dynamic subqery in for loop cursor

    I created a process that puts all retrieved rows into a collection. Users can select on quite a few columns, say unit, last_name, shift, etc. When the query is built based on users' inputs, I can't get it working using a For Loop Cursor (code 3.). But I tried and found out that sample 1 is working while sample 2 is not.
    Can anyone please help? give advices or point to the right direction?
    Thanks very much in advance!!!
    DC
    sample 1, works:
    begin
    :P8_TEST_CURSOR := '';
    for c in ( select ename from emp ) loop
    :P8_TEST_CURSOR := :P8_TEST_CURSOR || ', ' || c.ename;
    end loop;
    end;
    sample 2, does not work:
    declare
    q varchar2(2000) := 'select ename from emp';
    begin
    :P8_TEST_CURSOR := '';
    for c in q loop
    :P8_TEST_CURSOR := :P8_TEST_CURSOR || ', ' || c.ename;
    end loop;
    end;
    code 3, my actually pursuing code, sorry it's long:
    declare
    q varchar2(2000);
    v_unit_id cpd_units.id%TYPE;
    begin
    q := 'select distinct e.user_id
    , nvl(to_char(law_common.get_star_no(e.id)), ''-'') star_no
    , nvl(e.last_nme, ''-'') last_name
    , nvl(e.first_nme, ''-'') first_name
    , nvl(e.MIDDLE_INITIAL, ''-'') middle_initial
    , e.employee_no
    , nvl(to_char(e.employee_position_cd), ''-'')
    , nvl(w.uod_cd, e.cpd_unit_assigned_no) unit
    , nvl(w.watch_cd, ''-'')
    from cpd_employees e, dpv2wtch w
    where e.status_i = ''Y''
    and nvl(e.resignation_date, sysdate) >= sysdate
    and e.employee_position_cd in (''9112'', ''9152'', ''9153'', ''9155'', ''9161'', ''9164'')
    and e.user_id is not null
    and w.ssn_no(+) = e.ssn
    and e.user_id not in (select c001
    from htmldb_collections
    where collection_name = ''ACTIVITY''
    if :P300_STAR_NO is not NULL then
    q := q || ' and e.id = law_common.get_employee_id(:P300_STAR_NO)';
    --q := q || ' and law_common.get_star_no(e.id) = :P300_STAR_NO';
    end if;
    if :P300_EMPLOYEE_NO is not NULL then
    q := q || ' and e.employee_no = :P300_EMPLOYEE_NO';
    end if;
    if :P300_USER_ID is not NULL then
    q := q || ' and e.user_id = :P300_USER_ID';
    end if;
    if :P300_LAST_NAME is not NULL then
    q := q || ' and upper(e.last_nme) like ''' || upper(:P300_LAST_NAME) || '%''';
    end if;
    if :P300_FIRST_NAME is not NULL then
    q := q || ' and upper(e.first_nme) like ''' || upper(:P300_FIRST_NAME) || '%''';
    end if;
    if :P300_EMPLOYEE_POSITION_CD is not NULL then
    q := q || ' and e.employee_position_cd = :P300_EMPLOYEE_POSITION_CD';
    end if;
    if :P300_UNIT is not NULL then
    q := q || ' and nvl(w.uod_cd, e.cpd_unit_assigned_no) = :P300_UNIT';
    end if;
    if :P300_WATCH_CD <> 'NULL' then
    q := q || ' and w.watch_cd = :P300_WATCH_CD';
    end if;
    --htp.p('Limit Unit '||:P0_LIMIT_UNIT);
    /* authorization of editing OWN, UNIT, or ALL recs */
    if substr(:P0_PERMISSION, 1, 5) = 'QUERY' then
    q := q || ' and e.user_id = v(''FLOW_USER'')';
    elsif substr(:P0_PERMISSION, -4, 4) = 'UNIT' then
    q := q || ' and coalesce(w.uod_cd, e.cpd_unit_assigned_no)= :P0_LIMIT_UNIT ';
    end if;
    htp.p('q: ' || q);
    cursor q_c is q;
    for c in ( q_c ) loop /* HOW CAN BE DYNAMIC ???? */
    l_seq_id := htmldb_collection.add_member(
    p_collection_name => 'OFFICER_ACTIVITY'
    , p_c001 => c.user_id
    , p_c002 => c.star_no
    , p_c003 => c.last_name
    , p_c004 => c.first_name
    , p_c005 => c.middle_initial
    , p_c006 => c.employee_no
    , p_c007 => c.employee_position_cd
    , p_c008 => c.unit
    , p_c009 => c.watch_cd
    , p_c050 => :P0_APP_USER
    end loop;
    end;

    Come back to solve my need. I get the working test process:
    declare
    type ty_cursor is ref cursor;
    my_cursor ty_cursor;
    my_rec emp%rowtype;
    l_num number := 7900;
    q varchar2(2000) ;
    l_using varchar2(200) := null;
    begin
    :P8_TEST_CURSOR := '';
    q := 'select * from emp';
    if :P8_ENAME is not null then
    q := q || ' where ename like :P8_ENAME';
    l_using := ':P8_ENAME';
    end if;
    if l_using is not null then
    open my_cursor for q using :P8_ENAME; --l_using;
    else
    open my_cursor for q;
    end if;
    loop
    fetch my_cursor into my_rec;
    exit when my_cursor%notfound;
    :P8_TEST_CURSOR := :P8_TEST_CURSOR || ', ' || my_rec.empno;
    end loop;
    end;
    But I need
    open my_cursor for q using l_using;
    to be working since my application has several searchable columns for users and I have to put users' input search into l_using. It does NOT work: q got 'select * from emp where ename like :P8_ENAME' and l_using got ':P8_ENAME' at "open ...." statement.
    How can I get this cursor opened dynamically?
    Thanks again!
    DC

  • How to fetch from cursor into plsql collection

    Dear Friends,
    I am trying to understand PLSQL collections. I am trying with the following example.
    CREATE OR REPLACE TYPE emp_obj AS OBJECT
    (     empname          VARCHAR2(100),     empjob          VARCHAR2(50),     empsal          NUMBER);
    CREATE OR REPLACE TYPE emp_tbl IS TABLE OF emp_obj;
    CREATE OR REPLACE PACKAGE eg_collection AS
    -- Delcare ref cursor
    TYPE rc IS REF CURSOR;
    -- Procedure
    PROCEDURE eg_collection_proc (out_result OUT rc);
    END;
    CREATE OR REPLACE PACKAGE BODY eg_collection AS
    PROCEDURE eg_collection_proc( out_result OUT rc) AS
    emp_tdt     emp_tbl := emp_tbl(emp_obj('oracle','DBA',100));
    CURSOR c2 IS SELECT ename,job,sal FROM emp WHERE sal > 2000;
    -- Declare a record type to hold the records from cursor and then pass to the collection
    emp_rec emp_obj;
    BEGIN
         OPEN c2;
         LOOP FETCH c1 INTO emp_rec;
              EXIT WHEN c1%NOTFOUND;
              emp_tdt.extend;
    emp_tdt(emp_tdt.count) := emp_rec;
         END LOOP;
         CLOSE c2;
    OPEN out_result FOR SELECT * FROM TABLE(CAST(emp_tdt AS emp_tbl));
    END eg_collection_proc;
    END eg_collection;
    Executing the proc
    variable r refcursor;
    exec eg_collection.eg_collection_proc(:r);
    print r;
    But I am getting compilation error type mismatch found at emp_rec between fetch cursor into variable

    I am trying to understand PLSQL collections. I dont why the code is not working
    SQL> CREATE OR REPLACE TYPE emp_obj AS OBJECT
    2 (
    3      empname          VARCHAR2(100),
    4      empjob          VARCHAR2(50),
    5      empsal          NUMBER
    6 )
    7 /
    Type created.
    SQL> CREATE OR REPLACE TYPE emp_tbl IS TABLE OF emp_obj
    2 /
    Type created.
    SQL> DECLARE
    2      emp_tdt emp_tbl := emp_tbl ();
    3 BEGIN
    4
    5      emp_tdt.extend;
    6      SELECT emp_obj(ename, job, sal) BULK COLLECT INTO emp_tdt
    7      FROM emp WHERE sal < 4000;
    8
    9      DBMS_OUTPUT.PUT_LINE ('The total count is ' || emp_tdt.count);
    10
    11      emp_tdt.extend;
    12      SELECT ename, job, sal INTO emp_tdt(1).empname, emp_tdt(1).empjob, emp_tdt(1).empsal
    13      FROM emp WHERE empno = 7900;
    14
    15      DBMS_OUTPUT.PUT_LINE ('The total count is ' || emp_tdt.count);
    16
    17 END;
    18 /
    The total count is 13
    The total count is 14
    PL/SQL procedure successfully completed.
    SQL> DECLARE
    2      emp_tdt emp_tbl := emp_tbl ();
    3 BEGIN
    4
    5      emp_tdt.extend;
    6      SELECT ename, job, sal INTO emp_tdt(1).empname, emp_tdt(1).empjob, emp_tdt(1).empsal
    7      FROM emp WHERE empno = 7900;
    8
    9      DBMS_OUTPUT.PUT_LINE ('The total count is ' || emp_tdt.count);
    10
    11      emp_tdt.extend;
    12      SELECT emp_obj(ename, job, sal) BULK COLLECT INTO emp_tdt
    13      FROM emp WHERE sal < 4000;
    14
    15      DBMS_OUTPUT.PUT_LINE ('The total count is ' || emp_tdt.count);
    16 END;
    17 /
    DECLARE
    ERROR at line 1:
    ORA-06530: Reference to uninitialized composite
    ORA-06512: at line 6

  • Need help with loop cursor

    Hi,
    I'm doing a data conversion and am fairly new to PL/SQL.
    I have a cursor and in the loop i have a select statement
    which returns ORA-01403(no data found). I need to skip this
    row in the cursor and continue with the next.
    BEGIN
    OPEN cur_fill_split;
    LOOP
    FETCH cur_fill_split into S_ASR,S_CLIENT_NO, S_DIRCODE, S_SPLIT_RATE, I_WAYS,
    S_BRANCH_NUMBER, S_BRANCH_NAME, S_DIV_NUMBER, S_DIV_NAME, S_ITEMCODE, S_LINE;
    EXIT WHEN cur_fill_split%NOTFOUND;
    select order_number INTO N_ORDER_NUMBER from order_header
    where cmr_number||client_number||dir_number = s_asr||s_client_no||s_dircode;
    SELECT SEQ INTO I_SEQ FROM ORDER_DETAIL
    WHERE ORDER_NUMBER = N_ORDER_NUMBER
    AND LINE_ORDER_NUMBER = TO_NUMBER(S_LINE);
    ********(errors on the above select)********
    END LOOP;
    CLOSE cur_fill_split;
    END;
    Thanks,
    Brian

    Hi,
    I think there r 2 methods, one is by giving begin - end inside the loop as shown below 1st ex:
    & the 2nd method is by selecting the record count & then based on that value executing further commands.
    I think 2nd method is more safer than the 1st method.
    Method 1:
    1)
    -- ---------------Procedure Begin------------------
    BEGIN
    OPEN cur_fill_split;
    LOOP
    FETCH cur_fill_split into S_ASR,S_CLIENT_NO, S_DIRCODE, S_SPLIT_RATE, I_WAYS,
    S_BRANCH_NUMBER, S_BRANCH_NAME, S_DIV_NUMBER, S_DIV_NAME, S_ITEMCODE, S_LINE;
    EXIT WHEN cur_fill_split%NOTFOUND;
    select order_number INTO N_ORDER_NUMBER from order_header
    where cmr_number||client_number||dir_number = s_asr||s_client_no||s_dircode;
    BEGIN
    SELECT SEQ INTO I_SEQ FROM ORDER_DETAIL
    WHERE ORDER_NUMBER = N_ORDER_NUMBER
    AND LINE_ORDER_NUMBER = TO_NUMBER(S_LINE);
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    NULL;
    END;
    END LOOP;
    CLOSE cur_fill_split;
    END;
    -- ---------------Procedure End------------------
    Method 2)
    By selecting the record count & based on that records, deciding the program control.
    -- ---------------Procedure Begin------------------
    declare
    rec_cnt number := 0;
    BEGIN
    OPEN cur_fill_split;
    LOOP
    FETCH cur_fill_split into S_ASR,S_CLIENT_NO, S_DIRCODE, S_SPLIT_RATE, I_WAYS,
    S_BRANCH_NUMBER, S_BRANCH_NAME, S_DIV_NUMBER, S_DIV_NAME, S_ITEMCODE, S_LINE;
    EXIT WHEN cur_fill_split%NOTFOUND;
    select order_number INTO N_ORDER_NUMBER from order_header
    where cmr_number||client_number||dir_number = s_asr||s_client_no||s_dircode;
    /* Here we are using cnt variable & checking for
    the record count, if count = 0, then it will skip */
    select count(*) into rec_cnt from ORDER_DETAIL
    WHERE ORDER_NUMBER = N_ORDER_NUMBER
    AND LINE_ORDER_NUMBER = TO_NUMBER(S_LINE);
    if rec_cnt > 0 then
    SELECT SEQ INTO I_SEQ FROM ORDER_DETAIL
    WHERE ORDER_NUMBER = N_ORDER_NUMBER
    AND LINE_ORDER_NUMBER = TO_NUMBER(S_LINE);
    end if;
    END LOOP;
    CLOSE cur_fill_split;
    END;
    -- ---------------Procedure End------------------
    Try it out & mail me
    Good luck

  • Open cursors in loop (cursor name as a variable)

    Hi,
    I have such a problem:
    In my procedure I have declared 200 cursors.
    Now I want to open,fetch and close them one by one in Loop .
    I'm thinking about some array with all cursor's names.
    That is what I have (maybe stupid example but just to explain the issue) :
    DECLARE
    v_empno VARCHAR2(10);
    v_empname VARCHAR2(10);
    Cursor c1 IS
    SELECT '1' EMPNO,'Adam' EMPNAME
    FROM dual;
    Cursor c2 IS
    SELECT '2' EMPNO,'John' EMPNAME
    FROM dual;
    BEGIN
    OPEN c1;
    LOOP
    FETCH c1 INTO v_empno, v_empname;
    EXIT WHEN c1%NOTFOUND;
    dbms_output.put_line (v_empno||' '|| v_empname);
    END LOOP;
    CLOSE c1;
    OPEN c2;
    LOOP
    FETCH c2 INTO v_empno, v_empname;
    EXIT WHEN c2%NOTFOUND;
    dbms_output.put_line (v_empno||' '|| v_empname);
    END LOOP;
    CLOSE c2;
    END;
    And that is what I want to achieve:
    DECLARE
    v_empno VARCHAR2(10);
    v_empname VARCHAR2(10);
    TYPE cursors_tab IS TABLE OF VARCHAR(5);
    t_cursors cursors_tab := cursors_tab('c1','c2');
    Cursor c1 IS
    SELECT '1' EMPNO,'Adam' EMPNAME
    FROM dual;
    Cursor c2 IS
    SELECT '2' EMPNO,'John' EMPNAME
    FROM dual;
    BEGIN
    for i in t_cursors.first .. t_cursors.last loop
    OPEN :t_cursors(i);
    LOOP
    FETCH :t_cursors(i) INTO v_empno, v_empname;
    EXIT WHEN t_cursors(i)%NOTFOUND;
    dbms_output.put_line (v_empno||' '|| v_empname);
    END LOOP;
    CLOSE :t_cursors(i);
    end loop;
    END;
    Is that possible ?? I'm trying to solve it but I'm loosing hope ;)

    DECLARE
    v_empno VARCHAR2(10);
    v_empname VARCHAR2(10);
    TYPE stmt_tab IS TABLE OF VARCHAR(32767);
    t_stmt stmt_tab := stmt_tab(
                                'SELECT ''1'' EMPNO,''Adam'' EMPNAME FROM dual',
                                'SELECT ''2'' EMPNO,''John'' EMPNAME FROM dual'
    t_cur sys_refcursor;
    BEGIN
    for i in t_stmt.first .. t_stmt.last loop
    OPEN t_cur for t_stmt(i);
    LOOP
    FETCH t_cur INTO v_empno, v_empname;
    EXIT WHEN t_cur%NOTFOUND;
    dbms_output.put_line (v_empno||' '|| v_empname);
    END LOOP;
    CLOSE t_cur;
    end loop;
    END;
    1 Adam
    2 John
    PL/SQL procedure successfully completed.
    SQL> SY.

  • Loop Cursor Fetch Insert Rows Into Global Temporay Table

    Hi friends,
    I want to know there where is the problem in my this query
    cursor c1 is select empno
    from scott.emp;
    b number;
    begin
    open c1;
         loop
         fetch c1 into b;
         exit when c1%notfound;
    insert into scheema.a1
    (a)
    values
    (b);
    commit;
         end loop;
         close c1;
    end;
    There is no any error displaying while proceeding this query but the table a1 which is (Global temporary table with ON COMMIT PRESERVE ROWS option enabled) does not gets any records. Kindly let me know the problem.
    Regards.

    Are you sure that the query returns date? Are you checking the table via the same session?
    SQL> create global temporary table a1 (
      a  number)
      on commit preserve rows
    Table created.
    SQL> declare
       cursor c1 is select empno
          from scott.emp;
       b number;
    begin
       open c1;
       loop
          fetch c1 into b;
          exit when c1%notfound;
          insert into a1
          (a)
          values
          (b);
          commit;
       end loop;
       close c1;
    end;
    PL/SQL procedure successfully completed.
    SQL> select * from a1
             A
          7369
          7499
          7521
          7566
          7654
          7698
          7782
          7788
          7839
          7844
          7876
          7900
          7902
          7934
    14 rows selected.

  • Open cursor for plsql table

    There's a way to open a cursor from a plsql table?

    Hello
    Have a look here:
    http://asktom.oracle.com/pls/ask/f?p=4950:8:13876292179522624220::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:666224436920,

  • Open cursor for PLSQL table of records

    Is it possible to open a cursor for all data in a PLSQL table of records?
    something like
    cursor c (p1 number) is select * from <plsqltab>
    where <plsqltab>.col = p1

    There is no such thing as a PL/SQL table. Yes, I know that many calls this structure in PL/SQL a table. And that is exactly where all this confusion stems from.. and trying to treat such a "table" as an Oracle table using SQL.
    The correct terms are dynamic array (indexed by integer) or dynamic associative array (indexed by varchar). And an array is nothing like a table ito RDBMS processing.
    Yes, you can run SQLs against arrays. But it is "expensive". Why? Because the data sits inside PL/SQL Engine. Not in the SQL Engine. The data is in a PL/SQL defined structure. Not a SQL defined structure.
    So.. the data needs to be shipped from the PL/SQL Engine to the SQL Engine and converted into a format that the SQL Engine can understand and use.
    Also, once shipped and converted the SQL structure is not indexed. Which means that the only option is a full table scan of that structure.
    So you need to ask yourself why do you want to use SQL against a PL/SQL array? As soon as you do that, you are saying "Hey, this PL/SQL table ain't good enough and I need to process it using SQL".
    So why then does that data sit inside a PL/SQL array and not in a SQL table?
    Oracle provides you with the ability to create temporary session tables. These can be indexed. SQL can be run against them without all the "expenses" that are associated with running SQL against a PL/SQL array.
    PL/SQL arrays is a great tool. But only when it is the right tool to use. When someone says he/she needs SQL to use this tool, then I question the choice of the tool. Make sure you use the right tool for the job.

  • Loop cursor question

    Hi all...
    Really stuck here. So, I have two tables.... one is passenger info, the other is flight info. The passengers have id numbers. When I enter a correct passenger num and flight num my program works fine. What Im trying to do is cause an error to appear if I enter an invalid flight or passenger number. I have a cursor set up for flight and one set up for passenger. I believe my error may be in the fetch? Can someone take a look?
    Thanks in advance
    CURSOR pass_cursor IS
    SELECT pnum, name, age FROM passengers
    WHERE pnum=V_pnum;
    CURSOR flights_cursor IS
    SELECT flnum, dest, hours, cost FROM flights
    WHERE flnum=v_flnum;
    OPEN pass_cursor;
    LOOP
    FETCH pass_cursor INTO v_pnum, v_name, v_age;
    EXIT WHEN pass_cursor%NOTFOUND;
    IF flights_cursor%ISOPEN THEN
    CLOSE flights_cursor;
    END IF;
    OPEN flights_cursor;
    LOOP
    FETCH flights_cursor INTO v_flnum,v_dest,v_hours,v_cost;
    EXIT WHEN flights_cursor%NOTFOUND;

    You don't need to loop through the cursors if you just need a validation on primary or unique keys.
    DECLARE
    CURSOR pass_cursor IS
    SELECT pnum, name, age
       FROM passengers
    WHERE pnum=V_pnum;
    CURSOR flights_cursor IS
    SELECT flnum, dest, hours, cost
       FROM flights
    WHERE flnum=v_flnum;
    -- <variable declaration>
    BEGIN
    OPEN pass_cursor;
    FETCH pass_cursor INTO v_pnum, v_name, v_age;
    IF pass_cursor%NOTFOUND THEN
    DBMS_OUTPUT.PUT_LINE('Invalid passenger id entered');
    CLOSE pass_cursor;
    -- RAISE exception;
    END IF;
    CLOSE pass_cursor;
    OPEN flights_cursor;
    FETCH flights_cursor INTO v_flnum,v_dest,v_hours,v_cost;
    IF flights_cursor%NOTFOUND THEN
    DBMS_OUTPUT.PUT_LINE('Invalid flight id entered');
    CLOSE flights_cursor;
    --RAISE exception;
    END IF;
    CLOSE flights_cursor;
    EXCEPTION
    WHEN OTHERS THEN
    -- Logic if any exception is raised 
    END;

  • Using sql type with cursor in plsql block

    I wanted to bulk collect columns in a table into a sqltype .I cannot use plsql type here since I am using it with table function in a merge statement.
    Sample code is like this
    create type t_type as object(empno number);
    create type tab_type as table of t_type;
    1 declare
    2  t1 tab_type;
    3 begin
    4  select empno bulk collect into t1 from emp;
    5 end;
    /I get an error like
    ERROR at line 4:
    ORA-06550: line 4, column 8:
    PL/SQL: ORA-00932: inconsistent datatypes: expected UDT got NUMBER
    ORA-06550: line 4, column 1:
    PL/SQL: SQL Statement ignored
    Pls suggest some alternative

    Hi,
    You cannot bulk collect into a table of objects like this.
    Are you going to use some DML operations on this SQL collections. If yes then you need SQL collections and objects. Otherwise simple PL/SQL objects and collections will do.
    Anyway, to give an alternative.
    This is one way of doing things;
    create table emp(emp_no number(10),emp_name varchar2(100));
    INSERT INTO EMP ( EMP_NO, EMP_NAME ) VALUES (
    1, 'A');
    INSERT INTO EMP ( EMP_NO, EMP_NAME ) VALUES (
    2, 'B');
    INSERT INTO EMP ( EMP_NO, EMP_NAME ) VALUES (
    3, 'C');
    commit;
    create type t_type as object(empno number(10),empname varchar2(100));
    create type n_type as table of number(10);
    create type v_type as table of varchar2(100);
    create type tab_type as table of t_type;
    declare
    t1 tab_type := tab_type();
    t2 n_type := n_type();
    t3 v_type := v_type();
    begin
    select emp_no,emp_name bulk collect into t2,t3 from emp;
    t1.extend(t2.count);
    for cnt in t2.first..t2.last
    loop
    t1(cnt):=t_type(t2(cnt),t3(cnt));
    end loop;
    for cnt in t1.first..t1.last
    loop
    dbms_output.put_line(t1(cnt).empno||'---'||t1(cnt).empname);
    end loop;
    end;
    Now if you do not require the DML operations then you can do the following;
    declare
    type n_type is table of number(10);
    type v_type is table of varchar2(100);
    type rec_type is record(empno n_type,empname v_type);
    t1 rec_type;
    begin
    select emp_no,emp_name bulk collect into t1.empno,t1.empname from emp;
    for cnt in t1.empno.first..t1.empno.last
    loop
    dbms_output.put_line(t1.empno(cnt)||'---'||t1.empname(cnt));
    end loop;
    end;
    regards,
    Dipankar.

  • Cursor in plsql

    Hi,
    I wrote this:
    declare
    asterisk emp.stars%type;
    sal number;
    empno emp.employee_id%type := &x;
    begin
    select nvl(round(salary/1000, 0), 0) into sal
    from emp
    where employee_id = empno;
    for i in 1..sal loop
    asterisk := asterisk ||' *';
    end loop;
    dbms_output.put_line(asterisk);
    end;
    and I want to do this for all employees not just for one.
    How can I do that?
    Thank you

    Hi,
    Try this:
    SQL> set serveroutput on
    SQL> ed
    Wrote file afiedt.buf
      1  BEGIN
      2  FOR I IN (select salary ,LPAD('*',NVL(ROUND(salary/1000),0),'*') ast from e
    mployees)
      3  LOOP
      4  DBMS_OUTPUT.PUT_LINE(I.salary||'    ' ||I.ast);
      5  END LOOP;
      6* END;
    SQL> /
    24000    ************************
    17000    *****************
    17000    *****************
    9000    *********
    6000    ******
    4800    *****
    4800    *****
    4200    ****
    12000    ************
    9000    *********
    8200    ********
    7700    ********
    7800    ********
    6900    *******
    11000    ***********
    3100    ***
    2900    ***
    2800    ***
    2600    ***
    2500    ***
    8000    ********
    8200    ********
    7900    ********
    6500    *******
    5800    ******
    3200    ***
    2700    ***
    2400    **
    2200    **
    3300    ***
    2800    ***
    2500    ***
    2100    **
    3300    ***
    2900    ***
    2400    **
    2200    **
    3600    ****
    3200    ***
    2700    ***
    2500    ***
    3500    ****
    3100    ***
    2600    ***
    2500    ***
    14000    **************
    13500    **************
    12000    ************
    11000    ***********
    10500    ***********
    10000    **********
    9500    **********
    9000    *********
    8000    ********
    7500    ********
    7000    *******
    10000    **********
    9500    **********
    9000    *********
    8000    ********
    7500    ********
    7000    *******
    10500    ***********
    9500    **********
    7200    *******
    6800    *******
    6400    ******
    6200    ******
    11500    ************
    10000    **********
    9600    **********
    7400    *******
    7300    *******
    6100    ******
    11000    ***********
    8800    *********
    8600    *********
    8400    ********
    7000    *******
    6200    ******
    3200    ***
    3100    ***
    2500    ***
    2800    ***
    4200    ****
    4100    ****
    3400    ***
    3000    ***
    3800    ****
    3600    ****
    2900    ***
    2500    ***
    4000    ****
    3900    ****
    3200    ***
    2800    ***
    3100    ***
    3000    ***
    2600    ***
    2600    ***
    4400    ****
    13000    *************
    6000    ******
    6500    *******
    10000    **********
    12000    ************
    8300    ********
    PL/SQL procedure successfully completed.
    SQL>Cheers,
    Avinash

  • Cursor for loop in PlSql

    while i was studying about cursor for loop i found this statement in the web
    "A cursor FOR loop implicitly declares its loop index as a %ROWTYPE record"
    for example an emp table contain following columns empno,ename,sal,hiredate,deptno
    and let us consider an cursor for loop as
    for rec in select empno,sal from emp loop
    if cursor for loop declare loop index as a %rowtype our cursor statement should include all the columns and follow the data type compatabulity.
    But here our cursor statement includes only few columns,if we use %rowtype we have to select all columns but here we are not doing this.
    Can anyone please explain what is happening in cursor for loop?

    for loop cursor is also like the simple explicite cursor..
    the cursor variable will hold only the columns that are given in select statement of the cursor..
    if u declare a cursor as %rowtype, then it should include all the columns of that particular table..
    otherwise u should use only %type for each and every column seperately only...

  • How to run the cursor loop once even if it is true for many times - urgent

    Hi,
    Say
    loop
    tdate='12-JAN-2005'
    loop
    cursor.......
    if '10-JAN-2005'<=tdate then
    fetch
    end loop
    tdate=tdate+1 month
    end loop;
    in the above query i want my cursor to run the loop only once at the first true of my condition....in the second run the tdate will be = '12-FEB-2005'....but my cursor loop should not go in..it has to comeout....
    how to do this....plz help very urgent

    in the above query i want my cursor to run the loop only once at the first true of
    my condition....in the second run the tdate will be = '12-FEB-2005'....but my
    cursor loop should not go in..it has to comeout....Simply control has been the cursor processed or not.
    Something like that
    SQL> declare
      2    tdate date := to_date('12-JAN-2005','DD-MON-YYYY');
      3    cursor cr is select * from dual;
      4    rt dual%rowtype;
      5    cursor_has_been_processed boolean := false;
      6  begin
      7   loop
      8     if to_date('10-JAN-2005','DD-MON-YYYY') <=tdate then
      9             if not cursor_has_been_processed then
    10                     open cr;
    11                     loop
    12                     fetch cr into rt;
    13                     exit when cr%notfound;
    14                     end loop;
    15                     close cr;
    16                     cursor_has_been_processed := true;
    17                     dbms_output.put_line('Cursor has been processed');
    18             end if;
    19     --DO something
    20     null;
    21          end if;
    22     dbms_output.put_line(tdate);
    23     tdate := add_months(tdate,1);
    24     exit when tdate >= to_date('12-MAY-2005','DD-MON-YYYY');
    25   end loop;
    26  end;
    27  /
    Cursor has been processed
    12-JAN-05
    12-FEB-05
    12-MAR-05
    12-APR-05
    PL/SQL procedure successfully completed.Rgds.

Maybe you are looking for

  • Request for info on fatal error handling and High-Precision Timing in J2SE

    Hi Could anyone please provide me some information or some useful links on fatal error handling and High-Precision Timing Support in J2SE 5.0. Thanks

  • E71 with Maps 2... should I stay or should I go......

    Currently in China... fantastic country...amazing cold spell... snow 3m thick in places... Europe appears the same... still a working off line version of maps would be very useful... I have an E71 phone with maps 2 and the chat seems to be I'm better

  • Norton 360

    Each update of Firefox is consistently incompatible with Norton 360 toolbar and identity safe. The two options are: 1.) Roll back to the last compatible update.Turn off automatic updates. 2.) Operate without the identity safe and remember/type in man

  • Clearing old AP names on WCS

    when I go to monitor/access points I see access point names that are not the real names. Some will be called AP0023.0425.05ba and when I look on my controller there is no AP0023.0425.05ba. How can I clear out the access point list and make it refresh

  • Oracle DB prerequisites for installing OWB?

    Hi, I am planning to install OWB for a prototype project for a client. But I wanted to make sure that whatever I do now can be repeated at the time of the actual project too. For other things, I see no problem, but installation remains one. I need to