Looping internal tables

Hi all,
I have an internal table filled with data. I need to take up a single record at a time and compare with all the other records of the internal table. This I need to do for all the records of the internal table.
Please tell me the best way of doing this.
Thx..
Paul

HI,
use
LOOP AT itab1.
READ table itab2 with key field1 = itab1-field1
                           field2 = itab1-field2.
ENDLOOP.
<b>OR</b>
LOOP AT itab1.
LOOP AT itab2.
     if itab1-field1 = itab2-field1 AND
        itab1-field2 = itab2-field2.
     endif.
ENDLOOP.
ENDLOOP.
Regards,
HRA

Similar Messages

  • Loop Internal Table Performance

    Hi I am from BW and not much familiar with ABAP. I have written the following code in BW. I have around 1.5 million records coming into BW and the code I have written needs to go through 1.5 million times. Hence the data load is taking long time. I would like someone to please go through the code below and suggest if any for performance improvement.
    data: i_datapackage like DATA_PACKAGE occurs 0 with header line,
          i_index like sy-tabix,
          j_index like sy-tabix,
          i_employee like /bi0/memployee occurs 0 with header line,
          i_begda like /bi0/memployee occurs 0 with header line.
          prev_job like i_employee-job,
          prev_position like i_employee-hrposition,
          curr_job like i_employee-job,
          curr_position like i_employee-hrposition,
          job_datefrom like i_employee-datefrom,
          position_datefrom like i_employee-datefrom,
          prejob_datefrom like i_employee-datefrom,
          prepos_datefrom like i_employee-datefrom,
          prejob_dateto like i_employee-datefrom,
          prepos_dateto like i_employee-datefrom.
    i_datapackage[] = DATA_PACKAGE[].
    sort i_datapackage by employee calmonth ascending.
    select * from /bi0/memployee into table i_employee.
    sort i_employee by employee dateto descending.
    i_begda[] = i_employee[].
    sort i_begda by employee datefrom ascending.
    delete adjacent duplicates from i_employee comparing employee.
    loop at i_datapackage.
    *collecting current job,position, previous job, position
        read table i_employee with key employee = i_datapackage-employee
        dateto ='99991231'.
        if sy-subrc = 0.
          curr_job = i_employee-job.
          curr_position = i_employee-hrposition.
        endif.
       read table i_begda with key employee = i_datapackage-employee.
       if sy-subrc = 0.
         prev_job = i_begda-/bic/zhr_pjob.
         prev_position = i_begda-/bic/zhr_ppost.
       endif.
    *end of collecting current job and position
    sort i_begda by employee datefrom descending.
    read table i_begda with key employee = i_datapackage-employee
    binary search.
    if sy-subrc = 0.
      i_index = sy-tabix.
    endif.
    read table i_begda index i_index.
    if i_begda-employee ne i_datapackage-employee.
      exit.
    endif.
    code for populating service award tenure
    if i_begda-/bic/zhr_srvdt is not initial.
    i_datapackage-/bic/zhr_hrtnr = sy-datum+0(4) -
    i_begda-/BIC/ZHR_SRVDt+0(4).
    if sy-datum4(4) lt i_begda-/bic/zhr_srvdt4(4).
       i_datapackage-/bic/zhr_hrtnr = i_datapackage-/bic/zhr_hrtnr - 1.
    endif.
    endif.
    code for current hire tenure
    if i_begda-/bic/zhr_curdt is not initial.
    i_datapackage-/bic/zhr_crtnr = sy-datum+0(4) -
    i_begda-/BIC/ZHR_CURDT+0(4).
    if sy-datum4(4) lt i_begda-/BIC/ZHR_CURDT4(4).
       i_datapackage-/BIC/ZHR_CRTNR = i_datapackage-/bic/zhr_crtnr - 1.
    endif.
    endif.
    code for current supervisor tenure
    if i_begda-/bic/zhr_csbdt is not initial.
    i_datapackage-/bic/ZHR_CSTNR = sy-datum+0(4) -
    i_begda-/BIC/ZHR_CSBDT+0(4).
    if sy-datum4(4) lt i_begda-/BIC/ZHR_CSBDT4(4).
       i_datapackage-/bic/ZHR_CSTNR = i_datapackage-/bic/ZHR_CSTNR - 1.
    endif.
    endif.
    code for previous supervisor tenure
    if i_begda-/bic/zhr_psbdt is not initial.
    i_datapackage-/bic/ZHR_PSTNR = sy-datum+0(4) -
    i_begda-/BIC/ZHR_PSBDT+0(4).
    if sy-datum4(4) lt i_begda-/BIC/ZHR_PSBDT4(4).
       i_datapackage-/bic/ZHR_PSTNR = i_datapackage-/bic/ZHR_PSTNR - 1.
    endif.
    endif.
    Code for populating Years in current job
    and current position
      sort i_begda by employee datefrom ascending.
        clear j_index.
        read table i_begda with key employee = i_datapackage-employee.
        if sy-subrc = 0.
          j_index = sy-tabix.
        endif.
        loop at i_begda from j_index.
          if i_begda-employee ne i_datapackage-employee.
            exit.
          elseif ( i_begda-job eq curr_job ) and
          ( curr_job is not initial ).
            job_datefrom = i_begda-datefrom.
            prejob_dateto = i_begda-datefrom - 1.
            exit.
          endif.
        endloop.
        loop at i_begda from j_index.
          if i_begda-employee ne i_datapackage-employee.
            exit.
          elseif ( i_begda-hrposition eq curr_position ) and
              ( curr_position is not initial ).
              position_datefrom = i_begda-datefrom.
              prepos_dateto = i_begda-datefrom - 1.
              exit.
          endif.
        endloop.
        loop at i_begda from j_index.
          if i_begda-employee ne i_datapackage-employee.
            exit.
          elseif ( i_begda-job eq prev_job ) and
             ( prev_job is not initial ) and
             ( i_begda-datefrom ne '10000101').
              prejob_datefrom = i_begda-datefrom.
              exit.
          endif.
        endloop.
        loop at i_begda from j_index.
          if i_begda-employee ne i_datapackage-employee.
            exit.
          elseif ( i_begda-hrposition eq prev_position ) and
             ( prev_position is not initial ) and
             ( i_begda-datefrom ne '10000101').
              prepos_datefrom = i_begda-datefrom.
              exit.
          endif.
        endloop.
    i_datapackage-/bic/zhr_cjtnr = sy-datum+0(4) -
    job_datefrom+0(4).
    if sy-datum4(4) lt job_datefrom4(4).
       i_datapackage-/bic/zhr_cjtnr = i_datapackage-/bic/zhr_cjtnr - 1.
    endif.
    if position_datefrom+0(4) is not initial.
    i_datapackage-/bic/zhr_cptnr = sy-datum+0(4) -
    position_datefrom+0(4).
    if sy-datum4(4) lt position_datefrom4(4).
       i_datapackage-/bic/zhr_cptnr = i_datapackage-/bic/zhr_cptnr - 1.
    endif.
    endif.
    if prejob_datefrom is not initial.
    i_datapackage-/bic/zhr_pjtnr = prejob_dateto+0(4) -
    prejob_datefrom+0(4).
    if prejob_dateto4(4) lt prejob_datefrom4(4).
       i_datapackage-/bic/zhr_pjtnr = i_datapackage-/bic/zhr_pjtnr - 1.
    endif.
    endif.
    if prepos_datefrom is not initial.
    i_datapackage-/bic/zhr_pptnr = prepos_dateto+0(4) -
    prepos_datefrom+0(4).
    if prepos_dateto4(4) lt prepos_datefrom4(4).
       i_datapackage-/bic/zhr_pptnr = i_datapackage-/bic/zhr_pptnr - 1.
    endif.
    endif.
    modify i_datapackage transporting /bic/zhr_hrtnr /bic/zhr_crtnr
    /bic/zhr_cstnr /bic/ZHR_PSTNR /bic/zhr_cjtnr /bic/zhr_cptnr
    /bic/zhr_pjtnr /bic/zhr_pptnr.
    endloop.
    DATA_PACKAGE[] = i_datapackage[].
    Thanks.

    i_datapackage[] = DATA_PACKAGE[].
    i_begda[] = i_employee[].
    How larger are these internal tables ?
    If large, then
    > sort i_begda by employee datefrom descending.
    > read table i_begda with key employee = i_datapackage-employee binary search.
    the resorting and
    the nested loops
    => will kill your performance
    Please reconsider the whole logic, I have only some hints
    + Binary search does not help anything if you have to sort everytime you use it.
    + Never use a sort of a larger table inside a larger loop, it is always avoidable!
    + Check whether you can break down the big piece into smaller one, try to
       do the break down in the database already to save memory
    =>  Please provide your code in better layout, and explain what you actually want
          to do.
    Siegfried

  • Internal Table logic2

    Dear Friends,
    I have an internal table A like below show, i am looping the interal table
    A and inside i am reading the internal table B to assign the field value STLKN
    based on PLNKN and modifying the internal table A. So for all (PLNKN) 5 values
    (STLKN) 1 is assiged. this is ok
    now my requirement is for the second 5 (PLNKN)value in internal table B , the value 5(STLKN) should assign by appending new records.
    when you see the internal table B contains two 5 PLNKN and
    two 7 value in PLNKN and contains different STLKN values.
    when i loop internal table A and modify only the first value is assigned.
    now remaining value to be appended for the second value 5 PLNKN
    if not understandable below i given example.
    WARPL ABNUM PLNKN STLKN Internal table A
    211-- 1 4--
    0
    211-- 9 4--
    0
    211-- 3 4--
    0
    211-- 5 4--
    0
    211-- 7 4--
    0
    211-- 4 5--
    1
    211-- 2 5--
    1
    211-- 8 5--
    1
    211-- 10 5--
    1
    211-- 6 5--
    1
    211-- 4 6--
    6
    211-- 8 6--
    6
    211-- 8 7--
    7
    example-here need to append----
    211-- 4 5--
    5 like this
    211-- 2 5--
    5
    211-- 8 5--
    5
    211-- 10 5--
    5
    211-- 6 5--
    5
    211-- 8 7--
    1
    PLLNR----
    PLNKN -
    STLKN Internal table B
    106900-- 4--
    0
    106900-- 5--
    1
    106900-- 5--
    5
    106900-- 6--
    6
    106900-- 7--
    7
    106900-- 7--
    1
    can anyone give the code for this. keep in mind the internal table B contains
    more than 2 or 3 values repeated in PLNKN.
    Thnaks in advance
    karthik

    Hi Karthik,
    Try this code below.
    i_a_temp[] = i_tab_a.
    Loop at i_a_temp into wa_a_temp.
         read table i_tab_b into wa_tab_b with key plnkn = wa_a_temp-plnkn.
         if sy-subrc eq 0.
            i_b_temp[] = i_tab_b[].
            delete i_b_temp where plnkn ne wa_a_temp-plnkn.
            describe table i_b_temp line v_lines.
            do v_lines times.
            read i_b_temp into wa_b_temp with key plnkn = wa_a_temp-plnkn
                                                                      stlkn = wa_a_temp-stlkn.
           if sy-subrc NE 0.
           wa_a_temp-stlkn = wa_b_temp-stlkn.
           append wa_a_temp to i_tab_a.
           endif.
            enddo.
       endif.
    endloop.
    Kindly give rewards if helpful!
    mariposa

  • Which system variable is reset at the exit of a loop of an internal table

    which system variable is reset at the exit of a loop of an internal table
    a)sy-loop b)sy-index c)sy-dbcnt d)sy-tabix

    You might want to revist your threads from Friday, and award points and mark as "Answered" or "Solved".
    Regards,
    Rich Heilman

  • Loop using index read ( internal table)

    Hi,
    I thought of impelementing loop at ITAB using read statements, Here it goes.
    REPORT Z_LOOP_IMPROVE.
    DATA : T_OUTPUT TYPE STANDARD TABLE OF MARC WITH HEADER LINE,
    L_TABIX TYPE SY-TABIX.
    DEFINE ILOOP.
    DO.
    IF SY-INDEX EQ '1'.
    READ TABLE &1 WITH KEY &2 = &3 BINARY SEARCH.
    IF SY-SUBRC <> 0.
    EXIT.
    ENDIF.
    ELSE.
    L_TABIX = SY-TABIX + 1.
    READ TABLE &1 INDEX L_TABIX.
    IF SY-SUBRC NE 0 OR &1-&2 NE &3.
    EXIT.
    ENDIF.
    ENDIF.
    END-OF-DEFINITION.
    DEFINE IENDLOOP.
    ENDDO.
    END-OF-DEFINITION.
    DATA : T1 TYPE I, T2 TYPE I.
    SELECT * FROM MARC INTO TABLE T_OUTPUT.
    SORT T_OUTPUT BY WERKS.
    GET RUN TIME FIELD T1.
    ILOOP T_OUTPUT WERKS '0001'.
    WRITE : / T_OUTPUT-MATNR, T_OUTPUT-WERKS.
    IENDLOOP.
    GET RUN TIME FIELD T2.
    T2 = T2 - T1.
    WRITE : / T2.
    *C----
    But sadly it takes more time than a normal loop with
    where condition.
    can anyone suggest some ideas to improve execution speed?

    You can use a binary read to get the first record. Then read each record sequentially until you have proccessed all the records that meet your critera. You will have to have the internal table sorted so that the binary search and subsequent reads will work:
          READ TABLE itab WITH KEY
            field = whatever
            BINARY SEARCH.
          IF sy-subrc = 0.
            itab_index = sy-tabix.
            DO.
              IF sy-subrc = 0.
                IF itab_data-field = whatever
                  itab_index = itab_index + 1.
                  READ TABLE itab_data INDEX itab_index.
                ELSE.
                  EXIT.
                ENDIF.
              ELSE.
                EXIT.
              ENDIF.
            ENDDO.
          ENDIF.
    Rob

  • Doubts with control break statements on internal table loops (AT/ENDAT)

    Hi, i've had a couple of doubts for a long while which I hope someone can clarify today:
    1) I know how to use the AT statements, however, i'm not sure I get correctly what this part of help regarding this commands means:
    <i>"The control level structure with internal tables is static. It corresponds exactly to the sequence of columns in the internal table (from left to right). In this context, the criteria according to which you sort the internal table are unimportant."</i>
    I've always sorted the internal table before the control break and it works that way. For example:
    SORT ITAB BY EBELN EBELP.
    LOOP AT ITAB.
      AT NEW EBELN.
    *   Code for the order header
      ENDAT.
    ENDLOOP.
    If I <b>don't</b> sort the internal table, it doesn't work! (i get dupplicated processing). In the example, if i have more than one register with the same EBELN and they're not consecutive, the header gets processed twice. I really don't get that part of the help text.
    2) I know this: <i>"At the start of a new control level (i.e. immediately after AT), the following occurs in the output area of the current LOOP statement:
    All character type fields (on the right) are filled with "*" after the current control level key.
    All other fields (on the right) are set to their initial values after the current control level key."</i>
    My doubt is: WHY is that this way? Because sometimes (most times) I need those fields INSIDE the statement! So when that happened i've solved it in one of three ways:
    LOOP AT ITAB INTO WA_ITAB.
      WA_ITAB_AUX = WA_ITAB.
      AT NEW FIELD.
        WA_ITAB = WA_ITAB_AUX.
    *   ...Rest of the code for the first register
      ENDAT.
    ENDLOOP.
    LOOP AT ITAB INTO WA_ITAB.
      AT NEW FIELD.
        READ TABLE ITAB INDEX SY-TABIX INTO WA_ITAB.
    *   ...Rest of the code for the first register
      ENDAT.
    ENDLOOP.
    * (Without AT)
    LOOP AT ITAB INTO WA_ITAB.
      IF WA_ITAB-FIELD <> FIELD_AUX.
        FIELD_AUX = WA_ITAB_FIELD.
    *   ...Rest of the code for the first register
      ENDIF.
    ENDLOOP.
    Is there any problem with this way of coding? Can be done better?
    Thank you very much in advance.

    Hi..,
    1)
    See if u sort the table on a field on which u r using AT ENDAT .. then all the records which are having the same value for that field will form a group or those reocrds will be at one place.. so when u sort the table for all the records  AT ENDAT  will get executed onli once..
    If u dont sort this table on this field then all these records will be at different places and in between there may be records with different value for this field.. so this AT ENDAT will get executed for each record !!
    2)
    No u cannot use the Right hand fields of the field in the table .. Because these AT events work as Group based operations... So till that field on which AT ENDAT is working it breaks that record into two groups.. One is the left hand fields including that field.. and right hand fields as another group.. and makes the right hand group as stars ****.  Thats y u can observe that even any one field in the left hand group changes the AT ENDAT will get executed  !!!!
    Hope u understood !!!
    regards,
    sai ramesh

  • How to LOOP AT dynamic internal tables.

    I have an internal table name declared in field I_DYN_TAB with me and I want to loop at this I_DYN_TAB.
    ( 4 example:
    DATA: I_DYN_TAB(10) VALUE 'I_TAB'.
    where I_TAB is my internal table with data in it.)
    I already tried couple of things.
    A. READ TABLE (I_DYN_TAB) WITH KEY I_DYN_TAB-FIELD1
    but, it is not allowing me to read it dynamically this way.
    B. LOOP AT (I_DYN_TAB). Again this is also not working.
    Another option I tried is to create a field symbol for the internal table, which again is not useful or may be I am doing it wrong.
    So if anybody can help me in solving this problem, it would be great.
    Waiting for reply.
    Regards
    Arpit

    U can use
    ASSIGN COMPONENT...
    Eg:
    FIELD-SYMBOLS: <dyn_field>,
                     <dyn_field1>,
                     <dyn_field2>,
                     <dyn_field3> TYPE ANY .
    * Test when UOM, 'KMEIN' is '%'
    *        <b>ASSIGN COMPONENT 'KMEIN' OF STRUCTURE p_line1 TO <dyn_field1>.</b>
    *        ASSIGN COMPONENT 'KBETR' OF STRUCTURE p_line1 TO <dyn_field2>.
    *        ASSIGN COMPONENT 'KBETR' OF STRUCTURE p_line1 TO <dyn_field>.
    * Test when Curr, 'KONWA' is '%'
            ASSIGN COMPONENT 'KONWA' OF STRUCTURE p_line1 TO <dyn_field1>.
            ASSIGN COMPONENT 'KBETR' OF STRUCTURE p_line1 TO <dyn_field2>.
            ASSIGN COMPONENT 'KBETR' OF STRUCTURE p_line1 TO <dyn_field>.
            IF <dyn_field1> = '%'.
              <dyn_field> =  <dyn_field2> / 10.
            ENDIF.
            if <dyn_field2> LE -1.
              <dyn_field> = <dyn_field> * -1.
            endif.
              INSERT p_line1 INTO TABLE p_output1.
    Hope this helps.

  • Loop at Conditions within internal table

    I have not been ABAPing long so primarily have been using keyword help...
    I am trying to loop round an itab and only select and process unique keys from within the table!
    However the table I am using to obtain the records has multiple records with the same reference and I am only interested in each unique occurrence of the IBASE_COMPONENT_GUID field.
    So there could be four items for one Appliance and four for another one. So of the eight records, I would only need to loop round and select the unique GUIDS.
    So there would only be two unique appliances I need to extract the records for.
    Basically I am trying to do something like this:
        LOOP AT GT_ITEM_DATA INTO GS_ITEM_DATA WITH UNIQUE KEY IBASE_COMPONENT_GUID.
    Is there a way to select each unique occurrence?

    Basically, I use the COLLECT statement when I need to get the unique field (character only) field from the internal table.
    Create a table with only one field, GUID.
    LOOP through your main table, assign its GUID to new table's field GUID
    Use COLLECT new_table.
    Other option is,
    Sort the table by GUID
    Loop through it and use AT NEW GUID or AT END OF GUID
    Fill out the table with this GUID.
    Regards,
    Naimesh Patel

  • Loop at internal Table inside Script

    Hi
       I am filling a table in a subroutine in a program  and calling it through 'Perform' from my Script,now the main issue is , How to display the table in my script ? 
                I want to loop through an internal table & display its content on my script , but i can't make changes in the calling program by any means.

    Hi Ajitabh ,
    With PERFORM inside SAPSCRIPT you can only pass individual fields and also get back individual fields .
    Check This
    http://help.sap.com//saphelp_470/helpdata/EN/d1/802fd3454211d189710000e8322d00/frameset.htm
    Only "USING" and CHANGING" options are allowed and that too only symbols available / defined in sapscript can be passed .
    Even if you populate an internal table in the program you are calling with "PERFORM" there is no way to pass this internal table back to sapscript , also in SAPSCRIPT there is no way to loop .
    If you are sure about the no of lines you are going to populate and all lines have only one column ( only one field ) you can try something like this .
    /: PERFORM GET_VALUE IN PROGRAM <ZNAME>
    /: USING &VAR1&
    /: USING &VAR2&
    /: CHANGING &ITAB1&
    /: CHANGING &ITAB2&
    /: CHANGING &ITAB3&
    /: CHANGING &ITAB4&
    /: ENDPERFORM
    Anothe way is to loop in the main print program and call WRITE_FORM . But I guess your main print program is a SAP std program which you dont want to copy and change.
    Cheers.

  • BRFplus: Problem updating values in an internal table in a loop expression

    Hi
    I'm looking into the loop expression type of BRFplus and I have come across a problem updating an internal table, I'm trying to create and populate the table using a loop, here is the functionality I'm trying to achieve:
    In a rule set create an internal MONTH_TBL table containing 12 rows of two columns: MONTH_NUM containing values 1 through 12 and MONTH_VAL containing an amount (initially 0,00 EUR in all 12 rows).
    After initializing the table traverse through SFLIGHT table and for each row add PRICE from the table to MONTH_VAL in the row of MONTH_TBL corresponding to the month of FLDATE field in SFLIGHT.
    The initialization of MONTH_TBL works as intended, as does the traversal of and retrieval of values from SFLIGHT. The problem however is the update of the internal table MONTH_TBL (defined as result data object for the function). I don't get an error, but the tables does not get updated, and I cannot seem to find out what the problem is. I would have attached an XML extract of the function + ruleset for information, but it dosen't seem like that is possible, I could e-mail it on request (for SAP employees with access to system QU5 the function is LOOP_TEST in application Z_KLAUS_TEST).
    I hope that this is sufficient information to understand the issue that I'm dealing with.
    best regards
    Klaus Stenbæk, KMD

    Hi Klaus,
    The Loop expression is part of NW 7.02 which is not yet released. When you experience the problem as part of a test you should have a contact at SAP for dealing with problems/errors. Usually SAP-internal messages are used for this purpose. Please clarify with your SAP contact how the model is.
    BR,
    Carsten

  • Loop fields in an internal table

    Dear experts,
    I have a scenario where I have an internal table (created in an end routine) c of felds that are both characteristics and key figures. I would like to do is to modify this table so that I keep the value of the charactersitics but I set the value of all the key figures to 0. How can I acomplish this and what is the best way? Is there for example a way to loop all the fields in the work area when I am looping though the internal table and then dependant on the data type, set the key figues = 0?
    Thanks!
    -Cathrin

    Hi Cathrin,
    It will not be possible to set the key figures to zero based on data type.
    You will have to set each key figure in the loop.
    Example:
    LOOP ITAB INTO WA_ITAB.
        WA_ITAB-KEYFIGURE1 = 0.
        WA_ITAB-KEYFIGURE2 = 0.
        MODIFY ITAB FROM WA_ITAB.
    ENDLOOP.
    Regards,
    Hemant Khemani

  • Count the Records in  an internal table without a loop.

    How do i count the number of records in an internal table without using the loop  statement.
    The Describe statement give the total no. of records in the internal table.But i want the count of the key fields in the internal table
    For eg
    Row Field
    1       A
    2       A
    3       A
    4       B
    5       B
    6       C
    Count of A = 3
    Count of B = 2
    Count of C = 1

    Dilip,
      I think isn't posible. Only 3 ideas:
    1ª, make other table "table2", similar to your actual table.
         table2[] = yourtable[].
         delete table2 where key <> 'A'.
         describe table table2 lines contA.
        But depending the amount of data, you may have problems in performance or memory consumption...
    2ª, make other table, only with the keys fields and a count field,
        data begin table2
           key_field1,
           cont type i.
        end data.
       when you append lines to yourdata, make a collect to this table:
        table2-keys = yourtable-keys.
        table2-cont = 1.
       collect table2.
       But only works if you can modify the program where data is appended, and you may be carefull when data is deleted :-(...
    3ª if your data if filled in a select, you may fill the table2 of point 2ª, making a similar select but:
       select keyfields count( * ) into table table2
      from ...
      where (the same you have to fill your original table...)
       group by keys.
    But you have the same problem is data is deleted later...
    I hope that any of the three is useful for you...
    Edited by: Diego Alvarez on Jan 5, 2010 5:09 PM

  • Dynamic query in where clause while looping in an internal table.

    Hi,
    Had a small question : How can i make a dynamic query for the WHERE clause while looping at an internal table.
    i want to implement a dynamic where clause query for the below example.
    it_cfx_col is an internal table and wa_cfx_col is a work area for it_cfx_col
      DATA :
      i_cfx_col TYPE TABLE OF cfx_col,
      wa_cfx_col LIKE LINE OF i_cfx_col.
    DATA : count TYPE i VALUE 0.
    DATA : l_where_clause TYPE string,
             l_where_clause2 TYPE string,
             l_name type string.
    l_name = 'NANDANOM'.
    l_scenario = 'collaboration'.
    LOOP AT it_cfx_col INTO wa_cfx_col
    WHERE CREATED_BY = l_name
    AND SCENARIO = l_scenario.
    count = count + 1.
    some business logic implemented using the work area wa_cfx_col
    endloop.
    Now i want to write a dynamic query for the where clause.
    DATA : count TYPE i VALUE 0.
      DATA : l_where_clause TYPE string,
             l_where_clause2 TYPE string,
             l_name type string.
    l_name = 'NANDANOM'.
    l_scenario = 'collaboration'.
      l_where_clause = 'CREATED_BY = l_name'.
      l_where_clause2 = 'AND SCENARIO = l_scenario'.
    if l_scenario is not initial.
      CONCATENATE  l_where_clause l_where_clause2
      INTO l_where_clause SEPARATED BY space.
    endif.
    LOOP AT i_cfx_col INTO wa_cfx_col
    WHERE (l_where_clause).
    count = count + 1.
    some business logic implemented using the work area wa_cfx_col
    endloop.
    when i compile this i get an error message as { Statement concluding with "...(l_where_clause)" ended unexpectedly}
    Even i changed the initilization of the variable  l_where_clause2 to [ l_where_clause2 = 'AND SCENARIO = l_scenario.'. ]
    added the end of line demarkation ".", but still i got the same error message.
    Is it a limtation in ABAP that i cannot write a dynamic query for the where clause while looping at an internal table?
    Regards,
    om

    Hi savita,
    there in no such 1 limitaion in abap for dynamic query .. i think the  error meassge is only beacuse of your synatx delcartaion.
    >> LOOP AT i_cfx_col INTO wa_cfx_col
       WHERE (l_where_clause).
       count = count + 1.
    some business logic implemented using the work     area    wa_cfx_col
       endloop.
    afted delclarataion also , in the where statement you should specify both the field name and value bname
       LOOP AT i_cfx_col INTO wa_cfx_col
       WHERE l_where_clause = 'CREATED_BY = l_name' .
       count = count + 1.
    hope it helps.
    regads
    priya.

  • Program times out while looping at internal table with huge records - Needs fine tuning suggestions

    Hi,
    I am trying to execute a report. It times out while looping at vbap internal table. This internal table has 140000  records and does the validation within this loop and geenrates data for the output. Due to this huge volume, the program times out when executed foreground.
    There are no nested loops, so I cannot apply 'Parallel Cursor' here.
    Is there any way I can fine tune the program so that it doesn't timeout? Is it possible to apply 'Parallel Processing' . If yes, how?
    Thanks,
    Pavan

    Hi Pavan ,
                  ->sort your internal table by all primary key for vbap.
                  ->Read a Record from the table (use your condition here)
                  ->if record satisfys your where condition ,get that record index .
                  ->loop the table from the index (without condition) .
                  its like parallel cursor only but for single loop .;-)
                  ->use field symbols ,wherever possible .
               if still dump is coming ,contact your basis team .
    regards,
    Krishna.

  • Looping at internal table is taking lots of time

    Hi,
    I have two internal tables IT_1 and IT_2. My requirement is as below:-
    For record in table IT_1 if no record is found in table IT_2 then appned to the third table IT_3.
    I am using the below code for the same.
      LOOP AT tbl_articles INTO tbl_articles.
        READ TABLE tbl_prix_art WITH KEY matnr = tbl_articles-matnr.
        IF sy-subrc <> 0.
          MOVE tbl_articles TO tbl_articles_2.
          APPEND tbl_articles_2.
        ENDIF.
        CLEAR : tbl_articles, tbl_articles_2.
      ENDLOOP.
    But is taking huge amount of time due to larger number of records in tbl_articles .
    Is there any other way through which i can get the same functionality.
    Regards
    Sachin

    Sachin,
    Some food for thought.
    Irrespective of how you define the table it has to go through the n number of rows in tbl_articles because that is the basic functioanlity.
    I think your READ TABLE is what is delaying your process here. So try a Binary Search as below.
    Line 1 of change --> SORT tbl_prix_art asc ASCENDING matnr. 
    LOOP AT tbl_articles INTO tbl_articles.
    Line 2 of change --> READ TABLE tbl_prix_art BINARY SEARCH  WITH KEY matnr = tbl_articles-matnr.
    IF sy-subrc 0.
    MOVE tbl_articles TO tbl_articles_2.
    APPEND tbl_articles_2.
    ENDIF.
    CLEAR : tbl_articles, tbl_articles_2.
    ENDLOOP.
    I am confident this will solve your long run times.
    Thanks
    Mani

Maybe you are looking for

  • Need replacement for function module "WWW_GET_SCRIPT_AND_HTML"

    Hi, We are upgrading the system  from 4.6c to ECC 6. "WWW_GET_SCRIPT_AND_HTML" is flagged as obsolete in the ECC 6 version.Can you please give me a replacement for the above FM.The code is as follows.......thanks in advance CALL FUNCTION 'WWW_GET_SCR

  • Suggestions for New Battery for 13-inch MacBook

    I have been having trouble with my battery for a long time now. Often the computer fails to recognize the battery at all and will turn off when unplugged from the adapter (which is also acting screwy, but that's a different story). I having been usin

  • 10g RAC - updates immediately reflected (blocks cleared from buffer cache)?

    We've recently upgraded to 10g RAC (4 nodes on linux using a netapp filer for storage) and we're seeing some issues with data in our database intermittently looking different than data in our application cache. we're trying to diagnose the issue (app

  • GPS is V E R Y inacurate!!! E75

    Hello all users of NOKIA GPS! Here I am with my problem... I have brand new NOKIA E75 When I start GPS, navigation shows my position 10-15 meters away from my real place, though I am freezed on one sertain place (without moving) for several minutes. 

  • Reg : a simple query -

    Hi All, I got stuck up with a simple requirement - I have 2 tables - X and Y. Suppose, X has 2 columns - Original_Literature_Number and Revised_Literature_Number Y has 2 columns - Literature_Number and Literature_Name Output i want is - Original_Lite