Select statement in a for loop

Hi all,
Can a select stmt be used in the body of the for loop/ nested for loop ?
I tries using (even if its very simple for loop) it gives the following error
PL/SQL: ORA-00933: SQL command not properly ended...
code is
DECLARE
CURSOR C1 is select 'Monday'  from dual
union all
select 'Tuesday' from dual
union all
select 'Wednesday' from dual
union all
select 'Thursday' from dual
union all
select 'Friday' from dual
union all
select 'Saturday' from dual
union all
select 'Sunday' from dual;
        type rec_info is record
        name varchar2(20),
        FNAME varchar2(20),
        LNAME varchar2(20)
        type ty_info is table of rec_info;
        info ty_info;
        type rec_abc is record
        day varchar2(3000)
        type ty_abc is table of rec_abc;
        abc ty_abc;
        Cursor C2 is
        select t.name, u.first_name, u.last_name
        from territories t, users u, territories_users tu
        where t.ID = tu.TERRITORY_ID
        and tu.USER_ID = u.ID ;
BEGIN
OPEN C1;
Loop
FETCH C1 into abc;
EXIT when C1%notfound;
DBMS_OUTPUT.PUT_LINE(abc);
END LOOP;
CLOSE C1;
OPEN C2;
FETCH C2 BULK COLLECT into info;
CLOSE C2;
       for i in info.first .. info.last
       LOOP
                   for j in abc.first .. abc.last
                   LOOP
                          select --info(i).name, info(i).FNAME, info(i),LNAME,'AM' "AM/PM",
                        to_char(c.name)||' '||ct.PRIMARY_ADDRESS_CITY||','||ct.PRIMARY_ADDRESS_STATE||','||ct.PRIMARY_ADDRESS_COUNTRY
                       from
                        territories t, territories_users tu,
                        users u, calls_users cu, calls c, 
                        calls_contacts cc, contacts ct    
                        where
                        tu.TERRITORY_ID = t.id
                        and t.name = info(i).name
                        and u.first_name = info(i).FNAME
                        and u.last_name = info(i).LNAME
                      and (c.date_start between trim(next_day(sysdate,abc(j).day)) and trim(next_day(sysdate,abc(j).day)))
                        and tu.USER_ID = u.id
                        and cu.USER_ID = u.id
                        and cu.CALL_ID = c.id
                        and to_char(c.time_start,'hh24') < '12'
                        and cc.CALL_ID = c.ID
                        and cc.CONTACT_ID = ct.id
                        and rownum < 2
                 END LOOP;
     END LOOP;
END;

"Can a select stmt be used in the body of the for loop/ nested for loop "
Yes.... but with an INTO part.....
select c1 , c2 , c3 into var1 , var2 , var3 from table1
Sim

Similar Messages

  • Statement error in for loop noobq

    Hi
    Could anyoe tell me why this wont work? Its supposed to count upp to tal in the first one and then count down from tal in the second and its the second one that dont work. I get a statement error for the text i put in between stars (supposed to be bold but i dont know..)
    thanks
    class r?knaa {
        public static void main(String[] args) {
            new r?knaa().run();
        void run() {
         int tal = Keyboard.nextInt ("Ange ett tal ? s? ska vi r?kna...(^_^): ");
         for (int upp = 1; upp <= tal; upp = upp + 1) {
             System.out.print ( upp+ " " );
         System.out.println (" ");
         System.out.println (" ");
         for (*tal*; tal > 0; tal = tal -1 ){
             System.out.print ( tal+ " " );
    }Edited by: gibitlib on Feb 19, 2010 3:02 PM

    jverd wrote:
    sharkura wrote:
    I assumed that it would decrement after the block of statements associated with the while loop, as is true with the for loop.Not sure what you mean by "as is true with the for loop", but I'm catching a hint of a very common misconception about the post-inc/dec operators.
    A LOT of people think that post-inc/dec means "last step in the statement" or "after everything else has been done" or something like that. That kind of thiiking is just black magic hocus pocus and serves no purposeI don't think I thiik like that, Jeff. I've never used a post-inc operator in a while loop. Certainly, if I felt a need to do so, I would test it, and try to understand why it works the way it does. Empirically, I know that given the following for loop:
    for ( int i = 0; i < limit; i++ )
      ... some statements
    }the incrementation of i occurs after the statements in the block execute. It may be specific to the for loop that the third statement in the for loop executes after the block of statements, in which case
    for ( int i = 0; i < limit; ++i )
      ... some statements
    }would work the same, and you can bet I will test that.
    I did make an assumption last night, and did not test it. That is not typical of me. I do resent somewhat the accusation of black magic hocus pocus. My last statement in that post was:
    sharkura wrote:
    I'll have to play with this some.Implicit in that remark was my intent to read more about pre/post decrementation and experiment until I more fully understood it. You have pointed out my lack of complete understanding, and, odd as it may seem, I appreciate that. I do log into these forums, partly, to improve my understanding of java.
    {?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How build where clause in select statement in FM for Virtual provider

    Hi
    I looking for example of FM for Virtual provider where I find code how assign to select statement "where" clause value from query variable.
    In following code how build t_r_custtype range and how assign value to it.
    CODE********************************
    TYPE-POOLS: abap.
    initialize
      CLEAR: e_t_data, e_t_msg.
    this is specific to infoprovider VIRTPROV
      CHECK i_infoprov = 'VIRTPROV'.
      FIELD-SYMBOLS: <l_s_sbook> TYPE sbook,
                     <l_s_data>  TYPE ANY.
      DATA: l_t_component TYPE abap_compdescr_tab,
            l_t_sbook     TYPE TABLE OF sbook.
    initialize
      CLEAR e_t_data.
    Data selection / only Business Customer
      SELECT * FROM sbook
        INTO CORRESPONDING FIELDS OF TABLE l_t_sbook
        WHERE custtype in t_r_custtype.
    ENDCODE********************************
    Thanks a lot
    Adam

    Hello,
    Would you like fill the ranges in Customer exit for BEx..? 
    If Yes. please refer the attachment for the whole code...
    "Sample code in Customer Exit in BEx"
    IF i_step = 2.
    CASE i_vnam.
    WHEN 'ZDAY_CX'.
    LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZDAY_IN'.
    CLEAR: l_s_range.
    ZT_DT1 = loc_var_range-low.
    ZT_DT2 = loc_var_range-HIGH.
    CALL FUNCTION 'DATE_CREATE'
    EXPORTING
    ANZAHL_JAHRE = 0
    ANZAHL_KALTAGE = 0
    ANZAHL_MONATE = '-1'
    ANZAHL_TAGE = 0
    DATUM_EIN = ZT_DT1
    DATUM_EIN_ULT = ' '
    ULTIMO_SETZEN = ' '
    IMPORTING
    DATUM_AUS = ZFIDAY .
    E_TT =
    E_ULTKZ =
    CALL FUNCTION 'DATE_CREATE'
    EXPORTING
    ANZAHL_JAHRE = 0
    ANZAHL_KALTAGE = 0
    ANZAHL_MONATE = '-1'
    ANZAHL_TAGE = 0
    DATUM_EIN = ZT_DT2
    DATUM_EIN_ULT = ' '
    ULTIMO_SETZEN = ' '
    IMPORTING
    DATUM_AUS = ZLSDAY.
    E_TT =
    E_ULTKZ =
    l_s_range-low = ZFIDAY .
    l_s_range-high = ZLSDAY .
    l_s_range-sign = 'I'.
    l_s_range-opt = 'EQ'.
    APPEND l_s_range TO e_t_range.
    ENDLOOP.
    *****************************************End*************************************
    **To get the From date (For Text Variable) as per the user input date interval range**
    WHEN 'ZR_S'.
    LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZDAY_IN'.
    CLEAR: l_s_range.
    ZT_DT1 = loc_var_range-low.
    ZT_DT2 = loc_var_range-HIGH.
    CALL FUNCTION 'DATE_CREATE'
    EXPORTING
    ANZAHL_JAHRE = 0
    ANZAHL_KALTAGE = 0
    ANZAHL_MONATE = 0
    ANZAHL_TAGE = 0
    DATUM_EIN = ZT_DT1
    DATUM_EIN_ULT = ' '
    ULTIMO_SETZEN = ' '
    IMPORTING
    DATUM_AUS = ZFIDAY .
    E_TT =
    E_ULTKZ =
    l_s_range-low0(2) = ZFIDAY6(2).
    l_s_range-low+2(1) = '.'.
    l_s_range-low3(2) = ZFIDAY4(2).
    l_s_range-low+5(1) ='.'.
    l_s_range-low6(4) = ZFIDAY0(4).
    l_s_range-sign = 'I'.
    l_s_range-opt = 'EQ'.
    APPEND l_s_range TO e_t_range.
    ENDLOOP.
    *****************************************End*************************************
    Please let me know if any clarification required..
    Rinku..

  • State Diagram inside For Loop

    I created a fairly involved for-loop with a flat sequence inside of it.  The sequence had about four events that occured, and since it was kind of ugly, I thought I'd try the State Diagram Toolkit to replace the majority of the code.  When I selected state diagram from the pallete, I was outside my for-loop.  I drew up the various states/transitions, then realized that all this code needed to be inside the for-loop, but LV won't let me place state diagram-generated code inside the for-loop.  I select everything related to the state diagram, but when I drag into the for-loop, only the comments come inside.  I even tried generating a new for-loop and placing the state code inside.  No luck. Any thoughts?

    "Any thoughts?"
    Yes but not any that will help.
    1) Just modify your SD so that the exit loops back to the start while there are still things to do. Forget the outer For loop.
    2) THe observations you reported are correct. When I first discovered these limitiations I said "That sucks!". Since then I have learned a bit and have grown to love the SDE.
    The SDE is JUST (note: "just" is a four letter word around here ) a LV application that ofers a very limited GUI consisting of a picture control that then scripts (see LAVA Scripting Forum for info on scripting) your SD for you. SCripting LV code is no easy task. My experiments with scripting has shown that I have to keep a database of the contents of the LV diagram my code is developing to keep track of what is where. Well it turns out that the structure of a LV diagram is a lot like the structure of a FP in that you start with a "root" object either the FP or the BD. THe BD then has structures on it, like seq's loops etc with unlited nesting possible.
    Now the SD created by the SDE is really just a fancy while loop with an case structure and code to support the driving enum. In order for you to be able to edit the SD the SDE needs to know what is htere already so it can show the diagram. It "KNOWS" by looking at its internal DB (I believe is stored inside of VI along with the enum.ctl) of the SD. To keep the DB updated, the SDE must be used to manipulate the SD.
    Stop rambling and guessing and get to the point!
    Moving the SD is just not supported by the SDE so it can not be done. To do so would require the SDE be able to "look" at the diagram, and repeat all of its work in the new location in the diagram.
    BTW: Thank you for trying out and asking about the SDE. the more talking we do on this subject, the sooner the SDE will be updated.
    My SDE wishlist (partial)
    Add comments to diagram from SDE screen.
    Add data structures to SD (shift registers) from SDE screen.
    Add "un-do".
    Select multiple objects on SDE screen and move or allign.
    Select multiple objects on SDE screen and do "create sub-State-Diagram".
    Allow watching more than one SDE in execution highlighting at the same time.
    Ben
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction

  • Latency is very high when SELECT statements are running for LONG

    We are a simple DOWN STREAM streams replication environment ( Archive log is shipped from source , CAPTURE & APPLY are running on destination DB).
    Whenever there is a long running SELECT statement on TARGET the latency become very high.
    SGA_MAX_SIZE = 8GB
    STREAMS_POOL_SIZE=2GB
    APPLY parallelism = 4
    How can resolve this issue?

    Is the log file shipped but not acknowledge? -- NO
    Is the log file not shipped? -- It is shipped
    Is the log file acknowledged by not applied? -- Yes...But Apply process was not stopped. it may be slow or waiting for something?
    It is 10g Environment. I will run AWR.. But what should i look for in AWR?

  • Button "Selected State" bug confirmed for Blu-ray on Mac

    I just confirmed today with Adobe tech support that, at the moment, menu button selected states are invisible on Blu-ray discs created with Encore on a Mac.
    Meaning, if you have 5 buttons linked to 5 different places, the buttons will all work, but you will have no idea what you're selected on- because they are invisible! There will be no "highlight" or "selected" state for the button. Once selected, you will see the "activated state" for the button for a split second, then it will take you to the proper target track. But again, no way of knowing what buttons are selected.
    They said if the video/menu assets are 4:3, the buttons work fine. Good to know, but a completely ridiculous suggestion.
    At the moment, there is no solution. They are "working on it."
    This is only a problem with Blu-ray projects on the Mac platform. Lucky me. To a hobbyist, this isn't a completely devastating problem- you can still watch the content of the disc. But this is enough of a problem for that I can't deliver it to clients like this. Wasted time and money troubleshooting this. This is a major problem.

    Adam. When you see the LAYERS window, click on and off on your highlights. There is a small white looking box. Deactivate and activate this. This will activate the selected and activated states. We had this problem a long time ago and found this to be the solution!
    DARREN

  • 11gR2 SELECT statement: new syntax for specifying partition key

    I have an Oracle 11gR2 table which is interval partitioned on a single NUMBER column.
    I am trying to query a single partition using the new SELECT ... FOR ( < partition key> ) syntax.
    I keep getting an ORA-00905 error as follows, any ideas what I am doing wrong?
    SQL*Plus: Release 11.2.0.2.0 Production on Wed Dec 12 10:34:36 2012
    Copyright (c) 1982, 2010, Oracle. All rights reserved.
    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    SQL> select * from rpt_dif_daily_instance_fact for ( 41215 )
    2 /
    select * from rpt_dif_daily_instance_fact for ( 41215 )
    ERROR at line 1:
    ORA-00905: missing keyword
    SQL> show sqlpluscompatibility
    sqlpluscompatibility 11.2.0

    Welcome to the forum!
    Thanks for providing your 4 digit Oracle version. You should always do that when you post since functionality may be different between versions.
    >
    I have an Oracle 11gR2 table which is interval partitioned on a single NUMBER column.
    I am trying to query a single partition using the new SELECT ... FOR ( < partition key> ) syntax.
    I keep getting an ORA-00905 error as follows, any ideas what I am doing wrong?
    SQL*Plus: Release 11.2.0.2.0 Production on Wed Dec 12 10:34:36 2012
    Copyright (c) 1982, 2010, Oracle. All rights reserved.
    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    SQL> select * from rpt_dif_daily_instance_fact for ( 41215 )
    2 /
    select * from rpt_dif_daily_instance_fact for ( 41215 )
    ERROR at line 1:
    ORA-00905: missing keyword
    >
    As bencol said you are not using the new syntax. See the SQL Language doc 'partition_extension_clause' for the syntax diagram that shows the syntax options.
    http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_10002.htm
    If you provide the partition name then the syntax is
    . . .PARTITION (partition)If you provide a partition key value the syntax is
    . . .PARTITION FOR (partition_key_value)

  • Is possible to connect the 2 SELECTS statements into 1 for my stored procdr

    ELSE IF(@number = 2)
    BEGIN
    SELECT COUNT([outlet #])
    FROM OM
    WHERE [address] LIKE '%' + @address + '%';
    SELECT [outlet #], [name], address, [city/town], [postal code], telephone
    FROM OM
    WHERE address LIKE '%' + @address + '%'
    END
    Would it be possible to obtain the # of records and records in one shot
    Thank u for your time

    I suppose it depends on what you are really talking about, and what database and driver you are using.
    A statement can return more than one result set - see Statement.getMoreResults().

  • A dynamic select statement as source for a shuttle item: use xmlagg

    Here is a suggestion:
    If you want to create a dynamic string of values for the source of a shuttle item, you can use the xmlagg trick.
    For example:
    select rtrim (xmlagg (xmlelement (n, e.ename|| ':') order by e.ename ).extract ('//text()'), ':') employee_name
    from emp e
    It will return s string of names: ADAMS:ALLEN:BLAKE:CLARK:FORD:JAMES:JONES:KING:MARTIN:MILLER:SCOTT:SMITH:TURNER:WARD
    Important is not to forget the "order by" clause inside the xmlagg.
    If you have installed the STRAGG function, you could use that one also.
    For example:
    select replace(stragg(e.ename),',',':') employee_name
    from (select ename
    from emp
    order by ename) e
    From Oracle 11gR2 there is another way: the LISTAGG function
    For example:
    select listagg(e.ename, ':') within group (order by e.ename) employee_name
    from emp e
    regards,
    Mathieu

    Hi Michael
    Check out this posting and KM;
    https://blogs.oracle.com/warehousebuilder/entry/odi_11g_simple_flexible_powerful
    Cheers
    David

  • SELECT statement for VBKD - FAE in FPLT

    HI,
    For CS Report - Need to find the Conform Business (AMC is there But Invoice is Pending ).
    For This -->
    I need to take the table flow as - FPLT --> VBKD --> All (like VBAK, VBAP etc..)
    Problem is -->
    SELECT statement
    INTO IT_VBKD
    FOR ALL ENTRIES IN IT_FPLT
    WHERE fplnr = gwa_fplt-fplnr
    is taking too much time to execute.
    1. All Entries are Pending for Invoice ( FPLT- FKSAF = 'A' )
    2. No entry in VBFA table for this criteria.

    Thanks Vinod,
    Yes, I check it.
    But, In Client's system VBAK-rplnr is always Initial.
    Actually, I have data like -->
    AMC for duration - 01.07.2010 to 30.06.2011
    For which I am taking Four Billing Cycles -
    1. 01.07.2010 to 30.09.2010 - billed on 01.09.2010
    2. 01.10.2010 to 31.12.2010 - billed on 01.12.2010
    3. 01.01.2011 to 31.03.2011 - Unbilled - Projected billing date 01.03.2011       "
    4. 01.04.2011 to 30.06.2011 - Unbilled - Projected billing date 01.06.2011       "
    I have to consider Case 3 & 4 (unbilled). How can I calculate details for it?
    Report is working Fine - if I select - Selection options from FPLT - But while taking it from Sales Order - It's going to TIME OUT at SELECT statement itself.
    (Because table FPLT has more than 10Lac entries - and all are fetched )
    Edited by: Priya.ABAP on Dec 6, 2010 11:47 AM

  • SELECT statement (urgent!)

    Could you please help me with the following statement?
    The FROM clause is incorrect but i don't know what i should write.
    Thank you in advance.
    Maria.
    DECLARE
    A NUMBER;
    CURSOR C1 IS
    SELECT TABLE_NAME, COLUMN_NAME
    FROM USER_TAB_COLUMNS
    WHERE NULLABLE='N' AND TABLE_NAME LIKE 'P_%';
    BEGIN
    FOR i IN C1
    LOOP
    SELECT COUNT(*) INTO A
    FROM i.TABLE_NAME
    WHERE i.COLUMN_NAME IS NOT NULL;
    END LOOP;
    END;
    /

    FOR-LOOP
    Whereas the number of iterations through a WHILE loop is unknown until the loop completes, the number of iterations through a FOR loop is known before the loop is entered. FOR loops iterate over a specified range of integers. The range is part of an iteration scheme, which is enclosed by the keywords FOR and LOOP. A double dot (..) serves as the range operator. The syntax follows:
    FOR counter IN [REVERSE] lower_bound..higher_bound LOOP
    sequence_of_statements
    END LOOP;
    The range is evaluated when the FOR loop is first entered and is never re-evaluated.
    As the next example shows, the sequence of statements is executed once for each integer in the range. After each iteration, the loop counter is incremented.
    FOR i IN 1..3 LOOP -- assign the values 1,2,3 to i
    sequence_of_statements -- executes three times
    END LOOP;
    The following example shows that if the lower bound equals the higher bound, the sequence of statements is executed once:
    FOR i IN 3..3 LOOP -- assign the value 3 to i
    sequence_of_statements -- executes one time
    END LOOP;
    By default, iteration proceeds upward from the lower bound to the higher bound. However, as the example below shows, if you use the keyword REVERSE, iteration proceeds downward from the higher bound to the lower bound. After each iteration, the loop counter is decremented. Nevertheless, you write the range bounds in ascending (not descending) order.
    FOR i IN REVERSE 1..3 LOOP -- assign the values 3,2,1 to i
    sequence_of_statements -- executes three times
    END LOOP;
    Inside a FOR loop, the loop counter can be referenced like a constant but cannot be assigned values, as the following example shows:
    FOR ctr IN 1..10 LOOP
    IF NOT finished THEN
    INSERT INTO ... VALUES (ctr, ...); -- legal
    factor := ctr * 2; -- legal
    ELSE
    ctr := 10; -- not allowed
    END IF;
    END LOOP;
    Iteration Schemes
    The bounds of a loop range can be literals, variables, or expressions but must evaluate to numbers. Otherwise, PL/SQL raises the predefined exception VALUE_ERROR. The lower bound need not be 1, as the examples below show. However, the loop counter increment (or decrement) must be 1.
    j IN -5..5
    k IN REVERSE first..last
    step IN 0..TRUNC(high/low) * 2
    Internally, PL/SQL assigns the values of the bounds to temporary PLS_INTEGER variables, and, if necessary, rounds the values to the nearest integer. The magnitude range of a PLS_INTEGER is -2**31 .. 2**31. So, if a bound evaluates to a number outside that range, you get a numeric overflow error when PL/SQL attempts the assignment, as the following example shows:
    DECLARE
    hi NUMBER := 2**32;
    BEGIN
    FOR j IN 1..hi LOOP -- causes a 'numeric overflow' error
    END LOOP;
    END;
    Some languages provide a STEP clause, which lets you specify a different increment (5 instead of 1 for example). PL/SQL has no such structure, but you can easily build one. Inside the FOR loop, simply multiply each reference to the loop counter by the new increment. In the following example, you assign today's date to elements 5, 10, and 15 of an index-by table:
    DECLARE
    TYPE DateList IS TABLE OF DATE INDEX BY BINARY_INTEGER;
    dates DateList;
    k CONSTANT INTEGER := 5; -- set new increment
    BEGIN
    FOR j IN 1..3 LOOP
    dates(j*k) := SYSDATE; -- multiply loop counter by increment
    END LOOP;
    END;
    Dynamic Ranges
    PL/SQL lets you determine the loop range dynamically at run time, as the following example shows:
    SELECT COUNT(empno) INTO emp_count FROM emp;
    FOR i IN 1..emp_count LOOP
    END LOOP;
    The value of emp_count is unknown at compile time; the SELECT statement returns the value at run time.
    What happens if the lower bound of a loop range evaluates to a larger integer than the upper bound? As the next example shows, the sequence of statements within the loop is not executed and control passes to the next statement:
    -- limit becomes 1
    FOR i IN 2..limit LOOP
    sequence_of_statements -- executes zero times
    END LOOP;
    -- control passes here
    Scope Rules
    The loop counter is defined only within the loop. You cannot reference it outside the loop. After the loop is exited, the loop counter is undefined, as the following example shows:
    FOR ctr IN 1..10 LOOP
    END LOOP;
    sum := ctr - 1; -- not allowed
    You need not explicitly declare the loop counter because it is implicitly declared as a local variable of type INTEGER. The next example shows that the local declaration hides any global declaration:
    DECLARE
    ctr INTEGER;
    BEGIN
    FOR ctr IN 1..25 LOOP
    IF ctr > 10 THEN ... -- refers to loop counter
    END LOOP;
    END;
    To reference the global variable in this example, you must use a label and dot notation, as follows:
    <<main>>
    DECLARE
    ctr INTEGER;
    BEGIN
    FOR ctr IN 1..25 LOOP
    IF main.ctr > 10 THEN -- refers to global variable
    END IF;
    END LOOP;
    END main;
    The same scope rules apply to nested FOR loops. Consider the example below. Both loop counters have the same name. So, to reference the outer loop counter from the inner loop, you must use a label and dot notation, as follows:
    <<outer>>
    FOR step IN 1..25 LOOP
    FOR step IN 1..10 LOOP
    IF outer.step > 15 THEN ...
    END LOOP;
    END LOOP outer;
    Using the EXIT Statement
    The EXIT statement lets a FOR loop complete prematurely. For example, the following loop normally executes ten times, but as soon as the FETCH statement fails to return a row, the loop completes no matter how many times it has executed:
    FOR j IN 1..10 LOOP
    FETCH c1 INTO emp_rec;
    EXIT WHEN c1%NOTFOUND;
    END LOOP;
    Suppose you must exit from a nested FOR loop prematurely. You can complete not only the current loop, but any enclosing loop. Simply label the enclosing loop that you want to complete. Then, use the label in an EXIT statement to specify which FOR loop to exit, as follows:
    <<outer>>
    FOR i IN 1..5 LOOP
    FOR j IN 1..10 LOOP
    FETCH c1 INTO emp_rec;
    EXIT outer WHEN c1%NOTFOUND; -- exit both FOR loops
    END LOOP;
    END LOOP outer;
    -- control passes here
    Joel P�rez

  • My select select statement seems not to work...

    Hello experts,
    I am having trouble with my select statement below. Here is what I need to do:
    1. loop at itab it_equz.
    2. get single asset number(ANLNR) and description(SHTXT) from table ITOB where EQUNR =_equz-equnr.
    3. get single asset number(ANLN1), asset subclass(ANLN2 and subnumber(ORD42 from table ANLA where its asset(anln1) equals to the asset fetched by ITOB. (no. 2 step) and DEAKT eq '00000000'
    4. Now, I would get all fields(select single) in table ANLC where ANLN1 equals to asset fetched by ANLA and ANLN2 equals to ANLN2 fetched by ANLA and AFABE eq '01'.
    5.endloop.
    Now, the problem is that I can't seem to get the right values in table ANLC. Now, my boss said that the values that I must get in ANLC must not be deactivated. So I did that by getting the required fields(no 3 step) in table ANLA where its DEAKT is equal to '00000000'.
    Now, my select statement for ANLC seems not to work properly since I have the proper where conditions. Anyway, below is my select statement that I did:
    LOOP AT it_equz.  "no 1 step
    SELECT SINGLE anlnr shtxt FROM itob   "no 2 step
              INTO (it_itob-anlnr, it_itob-shtxt)
              WHERE equnr = it_equz_dum1-hequi.
    MOVE it_itob-anlnr TO it_finaltab-asset_dum.
                MOVE it_itob-anlnr TO it_finaltab-asset.
                MOVE it_itob-shtxt TO it_finaltab-description.
    SELECT SINGLE bukrs anln1 anln2 ord42   "no 3 step
                FROM anla                                  
                INTO (it_anla-bukrs, it_anla-anln1, it_anla-anln2,
                      it_anla-ord42)
                WHERE anln1 = it_finaltab-asset
                  AND bukrs IN r_iwerk
                  AND deakt = '00000000'.
    MOVE it_anla-ord42 TO it_finaltab-asset_sub.
                  WRITE it_finaltab-asset TO v_asset.
                  CONCATENATE v_asset '-' it_anla-anln2
                  INTO it_finaltab-asset_subnum.
    SELECT SINGLE bukrs anln1 gjahr afabe knafa kansw  "no 4 step
                              answl kaafa nafal aafav aafag
                              nafap aafap nafav nafag FROM anlc
                          INTO (it_anlc-bukrs, it_anlc-anln1, it_anlc-gjahr,
                                it_anlc-afabe, it_anlc-knafa, it_anlc-kansw,
                               it_anlc-answl, it_anlc-kaafa, it_anlc-nafal,
                               it_anlc-aafav, it_anlc-aafag, it_anlc-nafap,
                               it_anlc-aafap, it_anlc-nafav, it_anlc-nafag)
                            WHERE anln1 EQ it_finaltab-asset
                              AND anln2 EQ it_anla-anln2
                              AND bukrs EQ it_anla-bukrs
                              AND afabe EQ '01'.
    it_finaltab-accq_cost = it_anlc-kansw + it_anlc-answl.
                IF p_posdep = 'X'.
                  it_finaltab-acc_dep = it_anlc-knafa +
                                        it_anlc-nafav +
                                        it_anlc-nafag +
                                        it_anlc-kaafa +
                                        it_anlc-aafav +
                                        it_anlc-aafag.
                ELSE.
                  it_finaltab-acc_dep = it_anlc-knafa +
                                        it_anlc-nafav +
                                        it_anlc-nafag +
                                        it_anlc-kaafa +
                                        it_anlc-aafav +
                                        it_anlc-aafag +
                                        it_anlc-nafap +
                                        it_anlc-aafap.
                ENDIF.
    APPEND it_finaltab. CLEAR it_finaltab.
              APPEND it_equz_dum2. CLEAR it_equz_dum2.
              CLEAR it_itob. CLEAR it_anla. CLEAR it_anlc.
    endloop.

    Hi,
      Please avoid selection in loop. it will lead to major performance issue.
    avoid move statements ; instead use assignment.
    1st :- select the entire data into internal table
    eg:-
    SELECT anlnr shtxt FROM itob
    INTO table it_itob
    for all entries in it_equz
    WHERE equnr = it_equz-hequi.
    1st select
    2nd select.
    2nd :- now loop on ur internal table to get the desired result.
    it is good to work with internal table rather than select statements in loop.
    Regards

  • Select Statement Hangs often

    Hello ,
    I have very strange issue, Any select statement on few tables takes very long time( in fact it hangs) often but the same SQL runs in a second all other times in the same database. Please note that it is a simple select statement without any 'for update' clause.
    After recycling the database, the select statement runs faster but this situation comes back after couple of days on the same set of tables.
    We are using 9i release 2.
    Can you please advise how to go about solving this type of issue.
    Thanks.

    438628, Robert is correct in that the board would need to see an explain plan to have any chance of narrowing the problem down. If bind variable peeking is involved the plan may not match what Oracle is actually doing to solve the query.
    When the problem occurs try looking in v$plan_table and see if the plan matches the explain plan output. A difference would be a clue that a reparse of sometype is being done so the plan could be different for each execution. The presence of child cursors would also point to something causing reparses.
    Do any of the columns for this table involved in the where clause has histograms on them? What is the value of the spfile/init.ora parameter cursor_sharing?
    If cursor_sharing is EXACT then bind variable peeking should not be the problem. If the parameter is SIMILAR or FORCE then it could be. On 9.2 I believe there is a bug that causes a reparse for every execution when SIMILAR is used so evey execution could be a different plan.
    To check for skewed data just count the values in the column and then count by value. If the max(value_count) / count(*) is greater than a small percentage of the data then the column is skewed. The worse the skew the more likely that a plan chosen for a distinctive value is not a good plan for a non-distinctive value.
    HTH -- Mark D Powell --

  • How to use the select statement in for loop

    Hi All,
    my question is can we use select statement in for loop like as follows .
    for key in select key from one_table.
    when i am using this am getting an error like Found select invalid i identifier
    how to make use of select statement in for loop
    please suggest me .
    Thanks
    Sree

    SQL>set serveroutput on;
    SQL> DECLARE
         BEGIN
         FOR Cur_Rec IN (SELECT dname FROM dept) LOOP
          DBMS_OUTPUT.PUT_LINE(Cur_Rec.dname);
         END LOOP;
         END;
    SQL>
    ACCOUNTING
    RESEARCH
    SALES
    OPERATIONSAs per your requirement always filter the Query beforehand
    Like
    FOR Cur_Rec IN (SELECT key FROM <table> WHERE key=1) LOOP
    END LOOP;Edited by: Lokanath Giri on १ दिसंबर, २०११ ३:५६ अपराह्न

  • Help with if statement in cursor and for loop to get output

    I have the following cursor and and want to use if else statement to get the output. The cursor is working fine. What i need help with is how to use and if else statement to only get the folderrsn that have not been updated in the last 30 days. If you look at the talbe below my select statement is showing folderrs 291631 was updated only 4 days ago and folderrsn 322160 was also updated 4 days ago.
    I do not want these two to appear in my result set. So i need to use if else so that my result only shows all folderrsn that havenot been updated in the last 30 days.
    Here is my cursor:
    /*Cursor for Email procedure. It is working Shows userid and the string
    You need to update these folders*/
    DECLARE
    a_user varchar2(200) := null;
    v_assigneduser varchar2(20);
    v_folderrsn varchar2(200);
    v_emailaddress varchar2(60);
    v_subject varchar2(200);
    Cursor c IS
    SELECT assigneduser, vu.emailaddress, f.folderrsn, trunc(f.indate) AS "IN DATE",
    MAX (trunc(fpa.attemptdate)) AS "LAST UPDATE",
    trunc(sysdate) - MAX (trunc(fpa.attemptdate)) AS "DAYS PAST"
    --MAX (TRUNC (fpa.attemptdate)) - TRUNC (f.indate) AS "NUMBER OF DAYS"
    FROM folder f, folderprocess fp, validuser vu, folderprocessattempt fpa
    WHERE f.foldertype = 'HJ'
    AND f.statuscode NOT IN (20, 40)
    AND f.folderrsn = fp.folderrsn
    AND fp.processrsn = fpa.processrsn
    AND vu.userid = fp.assigneduser
    AND vu.statuscode = 1
    GROUP BY assigneduser, vu.emailaddress, f.folderrsn, f.indate
    ORDER BY fp.assigneduser;
    BEGIN
    FOR c1 IN c LOOP
    IF (c1.assigneduser = v_assigneduser) THEN
    dbms_output.put_line(' ' || c1.folderrsn);
    else
    dbms_output.put(c1.assigneduser ||': ' || 'Overdue Folders:You need to update these folders: Folderrsn: '||c1.folderrsn);
    END IF;
    a_user := c1.assigneduser;
    v_assigneduser := c1.assigneduser;
    v_folderrsn := c1.folderrsn;
    v_emailaddress := c1.emailaddress;
    v_subject := 'Subject: Project for';
    END LOOP;
    END;
    The reason I have included the folowing table is that I want you to see the output from the select statement. that way you can help me do the if statement in the above cursor so that the result will look like this:
    emailaddress
    Subject: 'Project for ' || V_email || 'not updated in the last 30 days'
    v_folderrsn
    v_folderrsn
    etc
    [email protected]......
    Subject: 'Project for: ' Jim...'not updated in the last 30 days'
    284087
    292709
    [email protected].....
    Subject: 'Project for: ' Kim...'not updated in the last 30 days'
    185083
    190121
    190132
    190133
    190159
    190237
    284109
    286647
    294631
    322922
    [email protected]....
    Subject: 'Project for: Joe...'not updated in the last 30 days'
    183332
    183336
    [email protected]......
    Subject: 'Project for: Sam...'not updated in the last 30 days'
    183876
    183877
    183879
    183880
    183881
    183882
    183883
    183884
    183886
    183887
    183888
    This table is to shwo you the select statement output. I want to eliminnate the two days that that are less than 30 days since the last update in the last column.
    Assigneduser....Email.........Folderrsn...........indate.............maxattemptdate...days past since last update
    JIM.........      jim@ aol.com.... 284087.............     9/28/2006.......10/5/2006...........690
    JIM.........      jim@ aol.com.... 292709.............     3/20/2007.......3/28/2007............516
    KIM.........      kim@ aol.com.... 185083.............     8/31/2004.......2/9/2006.............     928
    KIM...........kim@ aol.com.... 190121.............     2/9/2006.........2/9/2006.............928
    KIM...........kim@ aol.com.... 190132.............     2/9/2006.........2/9/2006.............928
    KIM...........kim@ aol.com.... 190133.............     2/9/2006.........2/9/2006.............928
    KIM...........kim@ aol.com.... 190159.............     2/13/2006.......2/14/2006............923
    KIM...........kim@ aol.com.... 190237.............     2/23/2006.......2/23/2006............914
    KIM...........kim@ aol.com.... 284109.............     9/28/2006.......9/28/2006............697
    KIM...........kim@ aol.com.... 286647.............     11/7/2006.......12/5/2006............629
    KIM...........kim@ aol.com.... 294631.............     4/2/2007.........3/4/2008.............174
    KIM...........kim@ aol.com.... 322922.............     7/29/2008.......7/29/2008............27
    JOE...........joe@ aol.com.... 183332.............     1/28/2004.......4/23/2004............1585
    JOE...........joe@ aol.com.... 183336.............     1/28/2004.......3/9/2004.............1630
    SAM...........sam@ aol.com....183876.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183877.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183879.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183880.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183881.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183882.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183883.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183884.............3/5/2004.........3/8/2004............     1631
    SAM...........sam@ aol.com....183886.............3/5/2004.........3/8/2004............     1631
    SAM...........sam@ aol.com....183887.............3/5/2004.........3/8/2004............     1631
    SAM...........sam@ aol.com....183888.............3/5/2004.........3/8/2004............     1631
    PAT...........pat@ aol.com.....291630.............2/23/2007.......7/8/2008............     48
    PAT...........pat@ aol.com.....313990.............2/27/2008.......7/28/2008............28
    NED...........ned@ aol.com.....190681.............4/4/2006........8/10/2006............746
    NED...........ned@ aol.com......95467.............6/14/2006.......11/6/2006............658
    NED...........ned@ aol.com......286688.............11/8/2006.......10/3/2007............327
    NED...........ned@ aol.com.....291631.............2/23/2007.......8/21/2008............4
    NED...........ned@ aol.com.....292111.............3/7/2007.........2/26/2008............181
    NED...........ned@ aol.com.....292410.............3/15/2007.......7/22/2008............34
    NED...........ned@ aol.com.....299410.............6/27/2007.......2/27/2008............180
    NED...........ned@ aol.com.....303790.............9/19/2007.......9/19/2007............341
    NED...........ned@ aol.com.....304268.............9/24/2007.......3/3/2008............     175
    NED...........ned@ aol.com.....308228.............12/6/2007.......12/6/2007............263
    NED...........ned@ aol.com.....316689.............3/19/2008.......3/19/2008............159
    NED...........ned@ aol.com.....316789.............3/20/2008.......3/20/2008............158
    NED...........ned@ aol.com.....317528.............3/25/2008.......3/25/2008............153
    NED...........ned@ aol.com.....321476.............6/4/2008.........6/17/2008............69
    NED...........ned@ aol.com.....322160.............7/3/2008.........8/21/2008............4
    MOE...........moe@ aol.com.....184169.............4/5/2004.......12/5/2006............629
    [email protected]/27/2004.......3/8/2004............1631
    How do I incorporate a if else statement in the above cursor so the two days less than 30 days since last update are not returned. I do not want to send email if the project have been updated within the last 30 days.
    Edited by: user4653174 on Aug 25, 2008 2:40 PM

    analytical functions: http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/functions2a.htm#81409
    CASE
    http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/02_funds.htm#36899
    http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/04_struc.htm#5997
    Incorporating either of these into your query should assist you in returning the desired results.

Maybe you are looking for

  • Macbook Pro mid 2012 starting to lag when running more than one program.

    Firstly mah specs : Retina, Mid 2012 2.3 GHz Intel Core i7 8gb 1600 MHz DDR3 NVIDIA GeForce GT 650M / Intel HD 4000 OS X 10.8.4 (12E55) So, i bought this macbook about a year and a month ago and since i bought it everything has run fine, it has alway

  • Where can i get jdk 1.5 javadoc in Chm format?

    Where can i get jdk 1.5 javadoc in Chm format?

  • Mouse icon disappears!

    Sometimes when I am using photoshop CS3 or flash 9 standalone (and other applications too) the mouse icon disappears. In photoshop when I move my mouse out of the canvas it goes away, to get the pointer back I hit command-tab to bring up the app swit

  • How do you turn off the log file?

    How can I turn off the log file for WLS? Don't want the added overhead of logging during performance tests. Edwin

  • Command Key Not Working

    My Command (CMD) keys are not working on my iMac. It doesn't matter what application I am using and I've tried different keyboards with the same result. I checked my startup items and nothing is there. Login as different user, same problem. Potential