For loop - utl_file.put_line

I have this loop which I want to put the conditions as follows:
  for rec in C3(i.customer_id,i.co_id) = 'DP' and = 'CO') --conditions here
  loop
    utl_file.put_line( l_file,'Status is '||c3_rec.ohstatus|| ' ' ||i.co_id);  
    
    end loop;
   --   DBMS_OUTPUT.PUT_LINE('Status is '||c3_rec.ohstatus|| ' ' ||i.co_id);  
     END IF;
     
       utl_file.fclose( l_file );
How am I suppose to do?

Published by: JoannaLee on August 27, 2008 19:47

You don't want to open and close the file within the loop. It is extremely inefficient, the more it will overwrite data in the file each time that perform you an iteration in the loop (you can open the file in Add mode, rather than write mode, to solve the problem of the crash, but you would still have the performance issue).

You want that the code looks like

-- Open file
l_file := utl_file.fopen( ... );

-- Loop over your cursor
FOR c3_rec IN C3(i.customer_id,i.co_id)
LOOP
  -- Write a row to the file
  utl_file.put_line( ... );
END LOOP;

-- Close the file
utl_file.fclose( ... );

For simplicity, I am omitting exception handling logic, but I hope you see the general approach.

Justin

Tags: Database

Similar Questions

  • PLSQL utl_file.put_line error after the script running 8 ' e

    Hello
    I wrote a script to export the results of a query to an external file using utl_file.put_line. the script works very well for the first 8 hours.

    anonymous block filled
    anonymous block filled
    anonymous block filled
    anonymous block filled
    anonymous block filled
    anonymous block filled
    anonymous block filled
    anonymous block filled

    When I run the same script for the 9th time, I got an error message

    Error from the 1 in the command line:
    DECLARE
    OutFile utl_file.file_type;
    TYPE rc_a IS REF CURSOR;
    l_rc_a rc_a;
    TELLER_1 INTEGER: = 3;
    ctt1 varchar (2000);
    AA varchar (2000);
    BEGIN
    FOR TELLER_1 IN 3.9

    LOOP

    OutFile: = utl_file.fopen ('DAT_DIR', 'A1_W' |) TELLER_1 | "_KANS. TXT', 'w');

    ctt1: = '

    SELECT
    WORP_XW | ''|''||
    D | ''|''||
    V5_AANTAL_KANS | ''|''||
    V5_AVG_KANS | ''|''||
    V6_AANTAL_KANS | ''|''||
    V6_AVG_KANS | ''|''||
    V7_AANTAL_KANS | ''|''||
    V7_AVG_KANS | ''|''||
    V8_AANTAL_KANS | ''|''||
    V8_AVG_KANS | ''|''||
    V9_AANTAL_KANS | ''|''||
    V9_AVG_KANS
    Of
    (
    Select D, V5_AANTAL_KANS, V5_AVG_KANS, V6_AANTAL_KANS, WORP_XW, ROUND(V6_AVG_KANS,6) V6_AVG_KANS ROUND(V5_AVG_KANS,6),
    V7_AANTAL_KANS, V7_AVG_KANS, V8_AANTAL_KANS, ROUND(V8_AVG_KANS,6) V8_AVG_KANS ROUND(V7_AVG_KANS,6),
    V9_AANTAL_KANS, ROUND(V9_AVG_KANS,6) V9_AVG_KANS
    Of
    (
    Select WORP_XW, D, KAN,
    case VERSIE_VX when "V5" then 5 when "V6" when then 6 "V7" when then 7 "V8" when then 8 "V9" then 9 end as VERSIE_VX
    of VMENS. TOEVAL_BASIS_W' | TELLER_1 | »

    )
    pivot)
    AVG (KAN) as AVG_KANS,
    COUNT (KAN) AS AANTAL_KANS
    by VERSIE_VX (V5, V6, V7, V8, V9 9 8 7 6 5)
    )
    ORDER BY D
    )';

    L_rc_a OPEN FOR ctt1;
    LOOP
    EXTRACTION l_rc_a IN aa;
    EXIT WHEN l_rc_a % NOTFOUND;
    -dbms_output.put_line (aa);
    UTL_FILE.put_line (OutFile, aa, TRUE);
    END LOOP;

    END LOOP;
    UTL_FILE.fclose (outfile);
    END;
    Error report:
    ORA-29283: Ongeldige bestandsbewerking/ORA-29283: invalid file operation
    ORA-06512: in 'SYS. UTL_FILE", regel 536
    ORA-29283: Ongeldige bestandsbewerking/ORA-29283: invalid file operation
    ORA-06512: in rule 14
    29283 00000 - "invalid file operation.
    * Cause: An attempt was made to read from a file or a directory which is
    not exist, or the file or directory access was denied by the
    Operating system.
    * Action: Check access privileges to the file and directory on the file system
    and if reading, check that the file exists.


    The same is true when I use another script writes the results of a query to an external file. Y at - there someone who can tell me why I have an error message after running 8 ' e of this script.

    With respect,
    Michiel van Mens

    The second time through the outer loop, it will fail because you try to open a file that is already open. Move your utl_file.fclose statement of before the last "END LOOP '.

  • UTL_FILE.put_line cannot insert all rows

    As shown in the code below, I have for loops with the entrails of IF ELSE statement. How am I suppose to retrieve all rows in my csv file as currently, the csv file has only the last line inserted in the file?

    for c7_rec in C7 loop
    
    FOR C8_REC IN C8(c7_rec.PASSPORTNO) LOOP
    if l_loop8 = 0 then
    CASE  SIGN(C8_REC.AMOUNT) -- check amount negative or positive. If negative need refund to customer else customer owe starhub
         WHEN -1 THEN
    l_negative:=1;
    l_amount_negative:= C8_REC.AMOUNT;
    DBMS_OUTPUT.PUT_LINE(l_amount_negative);
    ELSE
    l_positive:=1;
    l_amount_positive:= C8_REC.AMOUNT;
    DBMS_OUTPUT.PUT_LINE(l_amount_positive);
     END case;
     
     IF l_positive=1 AND l_negative=1 THEN
     IF l_amount_positive < abs(l_amount_negative) then
     DBMS_OUTPUT.PUT_LINE('Positive smaller than negative');
    --l_amount_total:= l_amount_positive+l_amount_negative;
        utl_file.put_line(l_file,c8_rec.CUSTOMER_ID || ' ' ||C8_REC.CUSTOMER_ID|| ' ' ||l_amount_positive|| ' ' ||c8_rec.ohstatus);  
     Elsif  l_amount_positive > abs(l_amount_negative) THEN
     DBMS_OUTPUT.PUT_LINE('Positive bigger than negative');
     ELSE
      DBMS_OUTPUT.PUT_LINE('Nothing to offset');
     END IF; 
    --DBMS_OUTPUT.PUT_LINE(c8_rec.CUSTOMER_ID || ' ' ||C8_REC.CUSTOMER_ID|| ' ' ||C8_REC.AMOUNT|| ' ' ||c8_rec.ohstatus);
     --|| ' ' ||i.cscurbalance);
    l_loop8:=1;
    
    elsIF l_positive=1 AND l_negative=0 THEN
    
    --l_amount_total:= l_amount_positive+l_amount_negative;
        utl_file.put_line(l_file,c8_rec.CUSTOMER_ID || ' ' ||C8_REC.CUSTOMER_ID|| ' ' ||l_amount_positive|| ' ' ||c8_rec.ohstatus);  
    l_loop8:=1;
     elsIF l_positive=0 AND l_negative=1 THEN
    
    --l_amount_total:= l_amount_positive+l_amount_negative;
        utl_file.put_line(l_file,c8_rec.CUSTOMER_ID || ' ' ||C8_REC.CUSTOMER_ID|| ' ' ||l_amount_negative|| ' ' ||c8_rec.ohstatus);  
    l_loop8:=1;
     END IF;
     --DBMS_OUTPUT.PUT_LINE(C11_REC.AMOUNT||' '||C11_REC.PASSPORTNO||' '||C11_REC.OHSTATUS);
     end if;
     END LOOP;
    
        
        l_positive:=0;
     l_negative:=0;
         END LOOP;
            
       utl_file.fclose( l_file );
    CREATE OR REPLACE Procedure p_lee_test As  Cursor C7 Is    Select Unique Passportno      From j_lee_test;
    
      Cursor C8(p_Passportno12 Varchar2) Is    Select Unique Passportno,           Customer_Id,           Case             When Min_Sign = Max_Sign Then              Sum(amt) Over(Partition By Passportno Order By Passportno)             Else              amt           End Amount,           Ohstatus      From (Select Passportno,                   Customer_Id,                   amt,                   Min(Sign(amt)) Over(Partition By Passportno Order By Passportno) Min_Sign,                   Max(Sign(amt)) Over(Partition By Passportno Order By Passportno) Max_Sign,                   Ohstatus              From j_lee_test             Where Passportno = p_Passportno12)     Order By 1;
    
      l_Loop7           Integer := 0;  l_Loop8           Integer := 0;  l_Positive        Integer := 0;  l_Negative        Integer := 0;  l_File            Utl_File.File_Type;  l_Date            Date := Sysdate;  l_Amount_Positive Float;  l_Amount_Negative Float;  l_Amount_Total    Float;
    
    Begin  /*l_File := Utl_File.Fopen('UTL_TMP_DIR',                           'ZERORISATION_' || To_Char(l_Date, 'YYYYMMDD') ||                           '.csv',                           'w');*/  l_file :=  Utl_File.Fopen('/export/orautil/loguser/utl',                           'ZERORISATION_' || To_Char(l_Date, 'YYYYMMDD') ||                           '.csv',                           'w');  For C7_Rec In C7 Loop
    
        For C8_Rec In C8(C7_Rec.Passportno) Loop     -- If l_Loop8 = 0 Then        Case Sign(C8_Rec.Amount) -- check amount negative or positive. If negative need refund to customer else customer owe starhub          When -1 Then            l_Negative        := 1;            l_Amount_Negative := C8_Rec.Amount;            Dbms_Output.Put_Line(l_Amount_Negative);          Else            l_Positive        := 1;            l_Amount_Positive := C8_Rec.Amount;            Dbms_Output.Put_Line(l_Amount_Positive);        End Case;
    
            If l_Positive = 1 And l_Negative = 1 Then          If l_Amount_Positive < Abs(l_Amount_Negative) Then            Dbms_Output.Put_Line('Positive smaller than negative');            --l_amount_total:= l_amount_positive+l_amount_negative;            Utl_File.Put_Line(l_File,                              C8_Rec.Customer_Id || ' ' ||                              C8_Rec.Customer_Id || ' ' ||                              l_Amount_Positive || ' ' || C8_Rec.Ohstatus);          Elsif l_Amount_Positive > Abs(l_Amount_Negative) Then            Dbms_Output.Put_Line('Positive bigger than negative');          Else            Dbms_Output.Put_Line('Nothing to offset');          End If;          --DBMS_OUTPUT.PUT_LINE(c8_rec.CUSTOMER_ID || ' ' ||C8_REC.CUSTOMER_ID|| ' ' ||C8_REC.AMOUNT|| ' ' ||c8_rec.ohstatus);          --|| ' ' ||i.cscurbalance);          l_Loop8 := 1;
    
            Elsif l_Positive = 1 And l_Negative = 0 Then
    
              --l_amount_total:= l_amount_positive+l_amount_negative;          Utl_File.Put_Line(l_File,                            C8_Rec.Customer_Id || ' ' || C8_Rec.Customer_Id || ' ' ||                            l_Amount_Positive || ' ' || C8_Rec.Ohstatus);          l_Loop8 := 1;        Elsif l_Positive = 0 And l_Negative = 1 Then
    
              --l_amount_total:= l_amount_positive+l_amount_negative;          Utl_File.Put_Line(l_File,                            C8_Rec.Customer_Id || ' ' || C8_Rec.Customer_Id || ' ' ||                            l_Amount_Negative || ' ' || C8_Rec.Ohstatus);          l_Loop8 := 1;        End If;        --DBMS_OUTPUT.PUT_LINE(C11_REC.AMOUNT||' '||C11_REC.PASSPORTNO||' '||C11_REC.OHSTATUS);    --  End If;    End Loop;
    
        l_Positive := 0;    l_Negative := 0;  End Loop;
    
      Utl_File.Fclose(l_File);
    
    End;
    
    47817 47817 -.13 CO117587 117587 -17.5 CO240012 240012 -43.9 CM47265 47265 -489.76 IN47265 47265 220.24 DP
    
    your l_Loop8 = 0 for first time, but for other times its always 1...
    
  • Aliases for columns in explicit for LOOP sliders

    Hi friends

    In the below block (forloop) I don't get countsal values, but when I run the query, it gives the County. By this (COUNTSAL) the below if condition fails .i don't know why it is failing.

    But when I use it with a simple loop gives a correct result.

    FOR LOOP:

    SCR CL;

    SET SERVEROUTPUT ON;

    DECLARE

    CURSOR C1 IS EMPNO, ENAME, COUNT (SAL) SELECT COUNTSAL OF THE GROUP EMP BY EMPNO, ENAME;

    SHEIKYERBOUTI C1% ROWTYPE;

    BEGIN

    TO IND IN C1

    WHEN the OUTPUT C1% NOTFOUND;

    DBMS_OUTPUT. PUT_LINE (IND. EMPNO);

    DBMS_OUTPUT. PUT_LINE('SALCOUNT:'||) SHEIKYERBOUTI. COUNTSAL);

    IF SHEIKYERBOUTI. COUNTSAL > 0 THEN

    DBMS_OUTPUT. PUT_LINE ('NAME' |) SHEIKYERBOUTI. ENAME);

    END IF;

    END LOOP;

    END;

    simple loop; (get a correct output)

    SCR CL;

    SET SERVEROUTPUT ON;

    DECLARE

    CURSOR C1

    IS

    SELECT EMPNO, ENAME, COUNT (SAL) COUNTSAL OF THE GROUP EMP BY EMPNO, ENAME;

    SHEIKYERBOUTI C1% ROWTYPE;

    BEGIN

    OPEN C1;

    LOOP

    FETCH C1 INTO SHEIKYERBOUTI.

    WHEN EXIT C1% NOTFOUND;

    IF SHEIKYERBOUTI. COUNTSAL > 0 THEN

    DBMS_OUTPUT. PUT_LINE('NAMES:'||) SHEIKYERBOUTI. ENAME);

    END IF;

    END LOOP;

    END;

    Please suggest me.

    Your code is the missilg LOOP keyword. Also, loop for CURSOR automatically closes after extraction of all lines - no OUTPUT WHEN you have to. But the key is to loop to extract CURSOR loop lines said variable registration - a following KEYWORD, IND in your case:

    DECLARE

    CURSOR C1 IS EMPNO, ENAME, COUNT (SAL) SELECT COUNTSAL OF THE GROUP EMP BY EMPNO, ENAME;

    BEGIN

    TO IND IN C1 LOOP

    DBMS_OUTPUT. PUT_LINE (IND. EMPNO);

    DBMS_OUTPUT. PUT_LINE('SALCOUNT:'||) UNRELIABLE COUNTSAL);

    IF COUNTSAL > 0 THEN.

    DBMS_OUTPUT. PUT_LINE ('NAME' |) UNRELIABLE ENAME);

    END IF;

    END LOOP;

    END;

    /

    7521
    SALCOUNT: 1
    NAMESWARD
    7566
    SALCOUNT: 1
    NAMESJONES
    7844
    SALCOUNT: 1
    NAMESTURNER
    7876
    SALCOUNT: 1
    NAMESADAMS
    7499
    SALCOUNT: 1
    NAMESALLEN
    7369
    SALCOUNT: 1
    NAMESSMITH
    7782
    SALCOUNT: 1
    NAMESCLARK
    7839
    SALCOUNT: 1
    NAMESKING
    7698
    SALCOUNT: 1
    NAMESBLAKE
    7900
    SALCOUNT: 1
    NAMESJAMES
    7902
    SALCOUNT: 1
    NAMESFORD
    7788
    SALCOUNT: 1
    NAMESSCOTT
    7654
    SALCOUNT: 1
    NAMESMARTIN
    7934
    SALCOUNT: 1
    NAMESMILLER

    PL/SQL procedure successfully completed.

    SQL >

    SY.

  • PLSQL cursor for loop - no_data_found exception

    Hi all

    Please, could you help me. I start to learn PL/SQL, so please be pattient.

    the very simple block. Base table emp (for Oracle), he sucks with more then 5000 large salary. I thought that the result would be "aaa", but result is actually empty.

    declare
      cursor c1 (v_num number) is
        select ename,sal
        from emp
        where sal > v_num;
    begin
      for rec in c1(5000) LOOP
        begin
        dbms_output.put_line('Employee: ' || rec.ename || ' has a salary of ' || rec.sal);
      exception
      WHEN NO_DATA_FOUND THEN
        dbms_output.put_line('aaa ');
      end;
      END LOOP ;
    end;
    /
    
    

    How can I solve this problem?

    Thank you much for the help.

    With the help of a cursor for loop will never throw an exception no_data_found.

    Despite everything, you try to catch all exceptions thrown by dbms_output.

  • for loops with cursor

    Hello Experts,

    I'm a beginner in PLSQL. I use the oracle 11g on windows machine 7.

    I wrote a simple code that gives me an output.

    DECLARE
    v_dealer_name southern_orders.dealer_name%type;
    v_total_order southern_orders.total_order%type;
    cursor southern_cursor IS
    SELECT distinct (dealer_name), sum (total_order) OF THE group southern_orders by dealer_name;
    BEGIN
    OPEN southern_cursor.
    IF this is is southern_cursor % ISOPEN THEN OPEN southern_cursor;
    END IF;
    BECAUSE me in 1... LOOP 38
    EXTRACT the southern_cursor in v_dealer_name v_total_order;
    EXIT WHEN southern_cursor % NOTFOUND or southern_cursor % ROWCOUNT > 37.
    dbms_output.put_line (' the dealer is: ' | v_dealer_name |) "and their order is:' | '. v_total_order);
    END loop;
    close southern_cursor;
    end;

    the result is:

    the dealer is: has and their order is: 1000
    the dealer is: B and the order is: 2000
    the dealer is: c and the order is: 3000


    In the code above, I calculated the total number of lines, and then wrote the code. Instead, I would use the sql loop to the code.your above help is appreciated.
    DECLARE
        v_dealer_name southern_orders.dealer_name%type;
        v_total_order southern_orders.total_order%type;
        cursor southern_cursor
          IS
            SELECT  dealer_name,
                    sum(total_order) total_order
              FROM  southern_orders
              group by dealer_name;
    BEGIN
        FOR v_rec in southern_cursor LOOP
          dbms_output.put_line('the dealer is : ' || v_rec.dealer_name || ' and their order is :' || v_rectotal_order);
        END loop;
    end;
    /
    

    SY.

  • Question about the update of a cursor for loop

    Hi, please someone could help with the following:

    I have a simple script below:

    declare

    cursor c1 is select name, salary of employers;


    Start

    for rec loop c1

    dbms_output.put_line ('employee' |) Rec.Name | ' s former salary was £' | Rec.salary);

    update of employers set salary = salary * 1.2 where name = rec.name;


    dbms_output.put_line ('employee' |) Rec.Name | "new treatment is £' | Rec.salary);

    end loop;

    end;

    This gives me the old value of the salary, but the output for the new salary remains the same, that is to say, it has not increased by 1.2.

    Ive tried to run in the 2 loops that works, but does not give me in order I want, IE employers followed old sal nine.

    Ive tried to add is committed and various other things, but it makes no difference. I thought that the script above should work. Where I'm going wrong?

    Thank you very much

    882839 wrote:

    I learned (perhaps wrongly) that cursor for loops were good practice. Is it not the case?

    Could you mention who told you this?

    I'm curious because on a daily basis in almost all the sons of my treatment is slow , the reason for the slow transformation is the loop and all responses point out that you are using a loop ensures that the process is running more slowly as possible.

    But despite any response in this community saying loops are the worst practices, every day more people come here apparently having learned exactly the opposite and then wonder why their program runs like a dog three legs with an anchor attached to his tail.

  • How do I know the cursor for loop host variable contains data or not

    Hi all


    can someone tell me how to know cursor variable host loop for contains data or not.


    example:
    Start
    for curr_rec in (select * from double)
    loop

    dbms_output.put_line(curr_rec%ROWCOUNT);

    end loop;


    end;

    The following oracle error message is coming:

    ORA-06550: line 5, column 27:
    PLS-00324: cursor attribute can only be applied to non slider "CURR_REC."

    Please help me.

    Thank you and best regards,
    Prakash P

    Published by: 833560 on April 19, 2011 21:57

    833560 wrote:
    Hi all

    can someone tell me how to know cursor variable host loop for contains data or not.

    example:
    Start
    for curr_rec in (select * from double)
    loop

    dbms_output.put_line(curr_rec%ROWCOUNT);

    end loop;

    end;

    The following oracle error message is coming:

    ORA-06550: line 5, column 27:
    PLS-00324: cursor attribute can only be applied to non slider "CURR_REC."

    Please help me.

    Thank you best regards &,.
    Prakash P

    Published by: 833560 on April 19, 2011 21:57

    You know, in your code curr_rec is not slider.
    You cannot use curr_rec % rowcount
    If you want to get the number of lines of your loop, then use an additional variable

    Try this please

    declare
     countLoop NUMBER :=0;
    begin
      for curr_rec in( select * from dual)
      loop
        countloop := countloop + 1;
    
      end loop;
      dbms_output.put_line(countloop);
    end;
    

    or

    DECLARE
            CURSOR csr_org IS
                  SELECT empno, ename FROM   emp;
                  num_total_rows NUMBER;
          BEGIN
                  FOR idx IN csr_org LOOP
                      dbms_output.put_line(idx.empno||' '||idx.ename);
                      num_total_rows := csr_org%ROWCOUNT;
                  END LOOP;
                  IF num_total_rows > 0 THEN
                    dbms_output.new_line;
                    dbms_output.put_line('Total Organizations = '||to_char(num_total_rows));
                  END IF;
       END;
     /
    

    Published by: Mahir M. Quluzade, April 20, 2011 10:19

  • declareing cursor for loop inside another cursor for loop

    Hi friends,

    Is it possible declareing cursor loop within another cursor for loop for.

    Please guide for example?

    You can even declare cursor within the cursor loop

    declare
    cursor c1 is select deptno from the Department;
    BEGIN
    for cr1 in c1
    loop
    declare
    cursor c2 is select ename from emp where deptno = cr1.deptno;
    Start
    for cr2 in c2
    loop
    dbms_output.put_line (CR1. DEPTNO | ' ' || CR2.ename);
    end loop;
    end;
    end loop;
    end;

  • For loop stored procedure does not

    Hello

    I wrote a stored procedure that does not work (I mean do not display data)
    I set serveroutput on option also.
    If I run as a single SQL query its working very well. so please help

    create or replace procedure show_empnos (p_deptno number)
    is
    emp_id emp. EMP_ID % TYPE;
    cursor cur_emps is
    Select emp_id emp where department_id = '12';
    Start
    for a cur_emps in
    loop
    dbms_output.put_line (emp_id);
    end loop;
    end;


    See, when I ran that a single SQL interrogate data dosplayed

    SQL > select emp_id from emp where department_id = '12';

    EMP_ID
    ----------
    * 101 *.
    * 101 *.
    * 101 *.

    Help, please.

    You declare a local variable EMP_ID you initialize ever, so it will always be NULL.
    In your cursor, you also select a column EMP_ID.

    The line

    dbms_output.put_line(emp_id);
    

    solve the use of uninitialized local variable and then print a NULL value (which would appear as a blank line in the output). Assuming you want to return the value of the column EMP_ID your cursor, you need

    dbms_output.put_line(a.emp_id);
    

    As a general approach, including local variables whose names match exactly in the name of a column will create problems on the line where you write the code assumes that you speak for column only to discover that the identifier is resolved to the local variable instead. If you need a local variable to store the EMP_ID, therefore, it would make much more sense to declare it as

    l_emp_id emp.EMP_ID%TYPE;
    

    While it is not accidentally get confused with the name of the column.

    Finally, if department_id is a NUMBER, it should be compared to a number, not a string, i.e.

    WHERE department_id = 12
    

    Justin

  • Transpose the row of columns inside a FOR LOOP

    Hi all


    I'm on 11g R1 and I have 2 tables
     
    select * from table1; 
    
    PRODUCTNO   PRODUCT_NAME STATUS 
    --------------     -------------------- -------- 
             1          TV 5 
             2          CAR 5 
             3          COMPUTER 5 
            4      Accessories 5 
    
    select * from table2; 
    
    PRODUCTNONO                 PART_NAME 
    ---------------------       -------------------- 
              1                 ANTENNA 
              1                 CABLE 
              2                 BRAKES 
              2                 TYRES 
              2                 SPARK PLUGS 
              3                 MONITOR 
              4                 PRINTER 
              4                 KEYBOARD 
              5                 MOUSE 
    In a PL/SQL block, I have the cursor
     
    cursor cur_1 (P_name varchar2) is 
    select a.productno,a.product_name,a.status, b.part_name 
    From table1 a, table2 b 
           Where a.productno = b.productno 
    and b.part_name = P_name; 
    a l_col collection that stores the values of part_name little, something like

    ANTENNA
    CABLE,
    BRAKES,
    TIRES,
    SPARK PLUGS,
    MONITOR

    I now have in the pl/sql block
     
    for j in 1 .. l_col.count loop 
    For L_Carq In cur_1 (L_col(j).part_name) 
                    Loop 
                      Dbms_Output.Put_Line(L_carq.productno || ' | ' || 
                                             L_carq.product_name || ' | ' || 
                                             L_carq.part_name || ' | ' || 
                                             L_carq.status); 
                      Dbms_Output.Put_Line('-----------------------------------------------------------------------------------------------------------------------------------'); 
                        --/* 
                             Update table1   
                                  set status = 0 
                                  Where  productno = (l_carq.productno ); 
                             Commit; 
                        --*/   
                  End Loop; 
    and the dbms_output is
     
    PRODUCTNO PRODUCT_NAME    PART_NAME STATUS 
    ------------------ ----------------           -----------------   ------------ 
        1 TV            ANTENNA   5 
        1 TV            CABLE   5 
        2 CAR            BRAKES   5 
        2 CAR            TYRES   5 
        2 CAR            SPARK PLUGS   5 
        3 COMPUTER      MONITOR          5 
    You have an idea how I can get the dbms_output as
     
    
    PRODUCTNO PRODUCT_NAME    PART_NAME                STATUS 
    ------------------ -----------------------    -------------------                                 ---------- 
        1 TV            ANTENNA,CABLE 5 
        2 CAR            BRAKES,TYRES,SPARK PLUGS 5 
        3 COMPUTER      MONITOR                 5 
    On SQL, I could format the output very easy, but unfortunately I'm forced to use the loop for.

    Any help is appreciated.

    Thank you
    Dani

    The collection must be extended "manually":

    -- initializing with the default constructor (you can do it in the declaration section) :
    l_col := sys.odcivarchar2list();
    
    For K In D Loop
      l_col.extend;
      l_col(l_col.last) := k.part_name;
    End Loop;
    

    I can't specify each part_name in where clause

    Where do you source?
    If there is another table, you can use BULK COLLECT, but maybe I don't see the whole picture here?

    Published by: odie_63 on August 2, 2010 16:45

  • getting numbers 2,3,4 for loop

    Hello

    How to get number 2,3,4 cursor?

    If I use this query to get the numbers
    Cursor cur is 
    SELECT rownum r
     FROM all_objects
    WHERE ROWNUM BETWEEN 1 AND 4;
    gives 1,2,3,4
    but I want 2,3,4 numbers so that I can use these numbers in the loop for.
     
    Begin 
    for rec in cur  
      select min(dateid), max(dateid) 
       from date_calendar dc
      where dc.ccyymmdd >= TO_CHAR (TRUNC (TO_DATE (SYSDATE), 'YEAR'),'YYYYMMDD')
        and dc.ccyymmdd <= TO_CHAR (ADD_MONTHS (LAST_DAY (SYSDATE), -rec.r),'YYYYMMDD');
    Thank you
    Sandy

    As others have pointed out, there are many ways to write a cursor to return the 2, 3, 4.
    However, you can't really a cursor for loop through a set of integers,
    Just use "for r to 2.4 loop '. For example,.

    declare
     d1  varchar2(8);
     d2  varchar2(8);
    begin
     for r in 2..4 loop
      select TO_CHAR (TRUNC (SYSDATE, 'YEAR'),'YYYYMMDD')
           , TO_CHAR (ADD_MONTHS (LAST_DAY (SYSDATE), -r),'YYYYMMDD')
      into d1, d2
      from dual;
      dbms_output.put_line(d1||'   '||d2);
     end loop;
    end;
    /
    
    20100101   20100131
    20100101   20091231
    20100101   20091130
    

    Kind regards
    Bob

  • slider with a for loop

    Hi all

    I have a simple slider in my pl/sql procedure. I use the method for the slider of the loop and retrieve the values below.


    DECLARE

    VAR_A VARCHAR2 (100);

    CURSOR C1 IS
    SELECT DEPT_NAME, DEPT_NO OF THE DEPARTMENT;
    BEGIN

    FOR I IN C1
    LOOP
    VAR_A: = I.DEPT_NAME;
    END LOOP;

    -etc etc.
    -etc etc.

    EXCEPTION
    WHILE OTHERS THEN
    DBMS_OUTPUT. Put_line ('hello');

    END

    This workfine but if the DEPT table does not all records in it, it goes directly to the management of exceptions, and if there are all the other instructions after the FOR LOOP, they will not be executed. I know there is a method to use the cursor (instruction FETCH) more, but I want to know what is wrong by using the FOR LOOP.

    The strange thing about this example is, even if I catch the exception, the program stops and if the DEPT table has no recording errors.

    Any suggestions or tips are greatly appreciated.

    the program stops and if the DEPT table has no recording errors.

    This seems a little suspicious. Probably another error product which is now hidden simply because of your manager WHEN of OTHER of exceptions, that didn't come out the real error message.

    I recommend getting rid of the exception handler together and after the release of the thrown exception.

  • Cursor object and cursor for loop back

    I am beginner in Oracle and the serious database of programming in general. Right now I'm studying the sliders. I more or less understand how works when the cursor returns a record, but I can't understand why this does not work when the cursor return object and I use "cursor for loop.

    Here's the test code:

    CREATE or REPLACE TYPE typ_Test () AS OBJECT
    text VARCHAR2 (50)
    );

    CREATE TABLE t_Test to typ_Test;

    INSERT INTO t_Test VALUES (typ_Test('111'));
    INSERT INTO t_Test VALUES (typ_Test('222'));

    -work, simple loop
    DECLARE
    CURSOR cur IS SELECT VALUE (o) FROM t_Test o;
    typ_Test obj;
    BEGIN
    Heart OPEN;
    LOOP
    Fetch cur INTO obj;
    OUTPUT WHEN heart % NOTFOUND;
    dbms_output.put_line (obj. (Text);
    END LOOP;
    CLOSE cur;
    END;



    -doesn't work, cursor for loop
    DECLARE
    CURSOR cur IS SELECT VALUE (o) FROM t_Test o;
    BEGIN
    FOR news of obj IN LOOP
    dbms_output.put_line (obj. (Text);
    END LOOP;
    END;



    Error report:
    ORA-06550: line 5, column 30:
    PLS-00302: 'TEXT' element must be declared.
    ORA-06550: line 5, column 5:
    PL/SQL: Statement ignored
    06550 00000 - "line %s, column % s:\n%s".
    * Cause: Usually a PL/SQL compilation error.


    EDIT:
    In fact I found no examples on the net of its use "cursor for loop" with cursor that returns the object. Only when the record is returned...

    Published by: user10393567 on February 10, 2009 07:38

    user10393567 wrote:
    Now, in the 'loop' example for what looks "obj" is a folder, not an object, and 'x' is attribute of this recording... But the cursor must return an object, not a record... What Miss me?

    In the FOR loop "obj" is a pointer to the cursor/query, not a subject in its own right. It gives you just a reference in the query.

    So with that you are referring to the query, but then you need to refer to the column in the query that you are interested, so why there need a name (in my case I called it 'x'). 'x' is the object, and then you refer to the attribute of this object "text".

    In the first request that you simply select the value directly object in a variable, so no matter what it's called in the query values are returned by the position that is the first column in the query goes into the first variable in the INTO clause. Once you have that then the variable itself contains the object, and you can just refer to the attribute within that.

    Hope that makes sense.

  • parallel processing: for loop taking place

    Hallo,

    I have a loop 'for' runnin on this system: LabVIEW2009, windows7, intel i7.

    I wonder if and how I can tell to LabVIEW that he would be allowed to run different iterations of the loop at the same time (concurrently) on my processor with 8 cores.

    Now, it will execute the next iteration when it ended the previous, even one so is there no dependencies in iterations.

    I want to give a 'ownership' of the loop, which means that it can be "unfolded" for parallel execution.

    Thank you

    Pier

    pop up on for loop and select "configure interation Prallelism."

    Activate it.

    Wire a certain number to the new 'P' of entry to specify how many processors to spread it everywhere.

    Ben

Maybe you are looking for