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

Similar Messages

  • Reg : internal table & Perform statement

    hi all
         i have 3 internal tables ,
    *     in first internal table i have mark-1 ,*
    *     in 2nd internal table i have mark-2 ,*
    *     using "PERFORM"  i have to add mark 1 & mark 2*
    *     and i need to display the total in  3rd internal table.*
    *     how to do by using "PERFORM" .*
                    Kindly clarify.
    with kind regards
    vel

    Raghupathy is right you just have to write:
    porgram code......
    perform sum_of_tables tables itab1
                                               itab2
                                               itab3.
    program code....
    definition of form:
    form_sum_of_tables tables itab1
                                          itab2
                                          itab3.
    definiton type of witab1, witab2, witab3.
    loop itab1 into witab1.
    loop itab2 into witab2.
    witab3-total = witab1-mark1 plus witab2-mark2.
    append witab3 to itab3.
    endloop.
    endloop.
    endform.
    It will return itab3 which is sum of itab1 and itab2.

  • Processing in 2 internal tables -Performance wise better option

    Hi Experts,
    I have 2 internal tables.
    ITAB1 and ITAB2  both are sorted by PSPHI.
    ITAB1 has PSPHI  some more fields INVOICE DATE  and AMT
    ITAB2 has PSPHI  some more fields amount.
    Both itab1 and itab2 will always have same amount of data.
    I need to filter data from ITAB2 based invoice date given on selection screen.since ITAB2 doesnt have invoice date field.
    i am doing further processing to filter the records.
    I have thought of below processing logic and wanted to know if there is a better option performance wise?
    loop at ITAB1 into wa where invoice_date > selection screen date. (table which has invoice date)
    lv_index = sy-tabix.
    read table itab2 where psphi = wa-psphi and index = lv_index.
    if sy-subrc = 0.
    delete itab2 index lv_index.
    endif.
    endloop.

    Hi Madhu,
    My Requirement is as below could you please advice on this ?
    ITAB1
    Field   1 PSPHI ,    FIELD 2 INVOICE,  FIELD 3 INVOICE_DATE , FIELD4 AMT
                 15245,                       INV1,                           02/2011  ,  400
                  15245                       INV2                            02/2012  ,  430
    ITAB2
       Field   1 PSPHI ,    FIELD 2 PSNR,      FIELD 3 MATNR  , FIELD4 AMT
                 15245,                       PSNR1,                   X .          430
                  15245                       IPSNR2                    Y,          400
    When user enteres date on sel screen as 02/2011
    I want to delete the data from itab1 and itab2 for invoice date greater then 02/2011/
    If i delere ITAB1 for date > selection screen date.
    Loop itab1.
    delete itab2 where psphi in itab1 will delete both rows in above example because the field psphi which is common can be mutiple.
    endloop.
    Can you advice ?

  • 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

  • Question about XML mapping to ABAP internal table

    Hi experts.
    I'm trying to XML mapping. But it doesn't work well. Assume there are XML file as below.
    <HEADER>
      <ITEM>
        <FOO>123</FOO>
        <BAR>ABC</BAR>
      </ITEM>
      <ITEM>
        <FOO>456</FOO>
        <BAR>DEF</BAR>
      </ITEM>
    <HEADER>
    and I want to trasformation it as below.
    ITAB
    FOO       |      BAR
    123         |  ABC
    456         | DEF
    How could I trasformation using "call transformation"?
    Regards.

    Hi,
    REPORT  zind_xml_to_sap NO STANDARD PAGE HEADING.
    Data Declaration                                                    *
    DATA: client      TYPE REF TO if_http_client, "Interface
          host        TYPE string,
          port        TYPE string,
          proxy_host  TYPE string,
          proxy_port  TYPE string,
          path        TYPE string,
          scheme      TYPE i,
          xml         TYPE xstring,
          response    TYPE string.
    DATA: t_xml       TYPE smum_xmltb OCCURS 0 WITH HEADER LINE.  "XML Table structure used
                                                                  "for retreive and output XML doc
    DATA: g_stream_factory TYPE REF TO if_ixml_stream_factory.    "Interface
    DATA : return  LIKE  bapiret2 OCCURS 0 WITH HEADER LINE.      "XML Table structure used for retreive
                                                                  "and output XML doc
    Parameters                                                          *
    PARAMETER : p_add TYPE string LOWER CASE ,
                p_dfile   LIKE rlgrap-filename.
    AT Selection-Screen on value-request for file                       *
    AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_dfile.
    Get file
      PERFORM 100_get_file.
    Start-of-Selection                                                  *
    START-OF-SELECTION.
    Perform to upload xml data from URL to SAP internal table
      PERFORM 200_xml_upload.
      IF t_xml[] IS NOT INITIAL.
      Perform to Download data from Internal Table to a text file in local drive
        PERFORM 300_download.
        write : / 'Data Uploaded to Internal Table Successfully'.
        write : / 'XML Data Downloaded to Local path', p_dfile.
      else.
        write : / 'No Data for upload'.
      ENDIF.
    *if t_xml[] is INITIAL.
    WRITE : address, 'Given URl cannot be Converted' .
    else.
    LOOP AT t_xml .
       WRITE:  t_xml-cname, t_xml-cvalue.
    ENDLOOP.
    endif.
    *&      Form  get_file
          Get File
    FORM 100_get_file .
      CALL FUNCTION 'F4_FILENAME'
      EXPORTING
        PROGRAM_NAME        = SYST-CPROG
        DYNPRO_NUMBER       = SYST-DYNNR
        FIELD_NAME          = ' '
       IMPORTING
         file_name           = p_dfile
    ENDFORM.                    " 100_get_file
    *&      Form  200_xml_upload
          form to upload xml data from URL to SAP internal table
    FORM 200_xml_upload .
    *Check HTTP:// and concatenate
      IF p_add NS 'http://' OR p_add NS 'HTTP://'.
        CONCATENATE 'http://' p_add
                    INTO p_add.
      ENDIF.
    Fetching the address of the URL
      CALL METHOD cl_http_client=>create_by_url
        EXPORTING
          url    = p_add
        IMPORTING
          client = client.
    *Structure of HTTP Connection and Dispatch of Data
      client->send( ).
    *Receipt of HTTP Response
      CALL METHOD client->receive
        EXCEPTIONS
          http_communication_failure = 1
          http_invalid_state         = 2
          http_processing_failed     = 3
          OTHERS                     = 4.
      IF sy-subrc <> 0.
        IF sy-subrc = 1.
          MESSAGE 'HTTP COMMUNICATION FAILURE' TYPE 'I' DISPLAY LIKE 'E'.
          EXIT.
        ELSEIF sy-subrc = 2.
          MESSAGE 'HTTP INVALID STATE' TYPE 'I' DISPLAY LIKE 'E'.
          EXIT.
        ELSEIF sy-subrc = 3.
          MESSAGE 'HTTP PROCESSING FAILED' TYPE 'I' DISPLAY LIKE 'E'.
          EXIT.
        ELSE.
          MESSAGE 'Problem in HTTP Request' TYPE 'I' DISPLAY LIKE 'E'.
          EXIT.
        ENDIF.
      ENDIF.
    Get data of the xml to Response
      response = client->response->get_cdata( ).
    *FM converting the XML format to abap
      CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
        EXPORTING
          text   = response
        IMPORTING
          buffer = xml.
    *FM converting XMl to readable format to a internal table.
      CALL FUNCTION 'SMUM_XML_PARSE'
        EXPORTING
          xml_input = xml
        TABLES
          xml_table = t_xml
          return    = return.
    ENDFORM.                    " 200_xml_upload
    *&      Form  300_download
    *form to Download data from Internal Table to a text file in local drive
    FORM 300_download .
      DATA filename TYPE string.
      filename = p_dfile.
      CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename                        = filename
        WRITE_FIELD_SEPARATOR           = 'X'
      TABLES
        data_tab                        = t_xml
    EXCEPTIONS
       FILE_WRITE_ERROR                = 1
       NO_BATCH                        = 2
       GUI_REFUSE_FILETRANSFER         = 3
       INVALID_TYPE                    = 4
       NO_AUTHORITY                    = 5
       UNKNOWN_ERROR                   = 6
       HEADER_NOT_ALLOWED              = 7
       SEPARATOR_NOT_ALLOWED           = 8
       FILESIZE_NOT_ALLOWED            = 9
       HEADER_TOO_LONG                 = 10
       DP_ERROR_CREATE                 = 11
       DP_ERROR_SEND                   = 12
       DP_ERROR_WRITE                  = 13
       UNKNOWN_DP_ERROR                = 14
       ACCESS_DENIED                   = 15
       DP_OUT_OF_MEMORY                = 16
       DISK_FULL                       = 17
       DP_TIMEOUT                      = 18
       FILE_NOT_FOUND                  = 19
       DATAPROVIDER_EXCEPTION          = 20
       CONTROL_FLUSH_ERROR             = 21
       OTHERS                          = 22
      IF sy-subrc <> 0.
         MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
    ENDFORM.

  • Reg transfer of data from excel to internal table

    hi
    i need to upload datas from Excel sheet to Internal Table.For this i use d FM
    ALSM_EXCEL_TO_INTERNAL_TABLE.but the problem is if a particular field has no value in excel sheet,then its not  uploaded into internal table.then next field value has come to that blank place in d internal table. what i need is if the excel sheet has a blank value for a field then a blank value is added in d inetrnal table also.

    Use below program logic
    Program    : ZLWMI151_UPLOAD(Data load to ZBATCH_CROSS_REF Table)
    Type       : Upload program
    Author     : Seshu Maramreddy
    Date       : 05/16/2005
    Transport  : DV3K919574
    Transaction: None
    Description: This program will get the data from XLS File
                 and it upload to ZBATCH_CROSS_REF Table
    REPORT ZLWMI151_UPLOAD no standard page heading
                           line-size 100 line-count 60.
    *tables : zbatch_cross_ref.
    data : begin of t_text occurs 0,
           werks(4) type c,
           cmatnr(15) type c,
           srlno(12) type n,
           matnr(7) type n,
           charg(10) type n,
           end of t_text.
    data: begin of t_zbatch occurs 0,
          werks like zbatch_cross_ref-werks,
          cmatnr like zbatch_cross_ref-cmatnr,
          srlno like zbatch_cross_ref-srlno,
          matnr like zbatch_cross_ref-matnr,
          charg like zbatch_cross_ref-charg,
          end of t_zbatch.
    data : g_repid like sy-repid,
           g_line like sy-index,
           g_line1 like sy-index,
           $v_start_col         type i value '1',
           $v_start_row         type i value '2',
           $v_end_col           type i value '256',
           $v_end_row           type i value '65536',
           gd_currentrow type i.
    data: itab like alsmex_tabline occurs 0 with header line.
    data : t_final like zbatch_cross_ref occurs 0 with header line.
    selection-screen : begin of block blk with frame title text.
    parameters : p_file like rlgrap-filename obligatory.
    selection-screen : end of block blk.
    initialization.
      g_repid = sy-repid.
    at selection-screen on value-request for p_file.
      CALL FUNCTION 'F4_FILENAME'
           EXPORTING
                PROGRAM_NAME = g_repid
           IMPORTING
                FILE_NAME    = p_file.
    start-of-selection.
    Uploading the data into Internal Table
      perform upload_data.
      perform modify_table.
    top-of-page.
      CALL FUNCTION 'Z_HEADER'
      EXPORTING
        FLEX_TEXT1       =
        FLEX_TEXT2       =
        FLEX_TEXT3       =
    *&      Form  upload_data
          text
    FORM upload_data.
      CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
           EXPORTING
                FILENAME                = p_file
                I_BEGIN_COL             = $v_start_col
                I_BEGIN_ROW             = $v_start_row
                I_END_COL               = $v_end_col
                I_END_ROW               = $v_end_row
           TABLES
                INTERN                  = itab
           EXCEPTIONS
                INCONSISTENT_PARAMETERS = 1
                UPLOAD_OLE              = 2
                OTHERS                  = 3.
      IF SY-SUBRC <> 0.
        write:/10 'File '.
      ENDIF.
      if sy-subrc eq 0.
        read table itab index 1.
        gd_currentrow = itab-row.
        loop at itab.
          if itab-row ne gd_currentrow.
            append t_text.
            clear t_text.
            gd_currentrow = itab-row.
          endif.
          case itab-col.
            when '0001'.
              t_text-werks = itab-value.
            when '0002'.
              t_text-cmatnr = itab-value.
            when '0003'.
              t_text-srlno = itab-value.
            when '0004'.
              t_text-matnr = itab-value.
            when '0005'.
              t_text-charg = itab-value.
          endcase.
        endloop.
      endif.
      append t_text.
    ENDFORM.                    " upload_data
    *&      Form  modify_table
          Modify the table ZBATCH_CROSS_REF
    FORM modify_table.
      loop at t_text.
        t_final-werks = t_text-werks.
        t_final-cmatnr = t_text-cmatnr.
        t_final-srlno = t_text-srlno.
        t_final-matnr = t_text-matnr.
        t_final-charg = t_text-charg.
        t_final-erdat = sy-datum.
        t_final-erzet = sy-uzeit.
        t_final-ernam = sy-uname.
        t_final-rstat = 'U'.
        append t_final.
        clear t_final.
      endloop.
      delete t_final where werks = ''.
      describe table t_final lines g_line.
      sort t_final by werks cmatnr srlno.
    Deleting the Duplicate Records
      perform select_data.
      describe table t_final lines g_line1.
      modify zbatch_cross_ref from table t_final.
      if sy-subrc ne 0.
        write:/ 'Updation failed'.
      else.
        Skip 1.
        Write:/12 'Updation has been Completed Sucessfully'.
        skip 1.
        Write:/12 'Records in file ',42 g_line .
        write:/12 'Updated records in Table',42 g_line1.
      endif.
      delete from zbatch_cross_ref where werks = ''.
    ENDFORM.                    " modify_table
    *&      Form  select_data
          Deleting the duplicate records
    FORM select_data.
      select werks
             cmatnr
             srlno from zbatch_cross_ref
             into table t_zbatch for all entries in t_final
             where werks = t_final-werks
             and  cmatnr = t_final-cmatnr
             and srlno = t_final-srlno.
      sort t_zbatch by werks cmatnr srlno.
      loop at t_zbatch.
        read table t_final with key werks = t_zbatch-werks
                                    cmatnr = t_zbatch-cmatnr
                                    srlno = t_zbatch-srlno.
        if sy-subrc eq 0.
          delete table t_final .
        endif.
        clear: t_zbatch,
               t_final.
      endloop.
    ENDFORM.                    " select_data
    Reward points if it is helpful
    Thanks
    Seshu

  • Populating values to internal table created dynamically

    Hi,
    I am creating an internal table(it1) dynamically and assigned it to a field symbol. now i want to upload values
    which are present in a field of another internal table being populated from a Funct Module.Could you tell me
    how to assign those values across the fields of field symbol?
    loop at itab_char.
    LS_ALV_CAT-FIELDNAME = itab_char-charact.
    append LS_ALV_CAT to I_ALV_CAT.
    endloop.
    CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
      EXPORTING
        IT_FIELDCATALOG = I_ALV_CAT
      IMPORTING
        EP_TABLE        = D_REF.
    ASSIGN D_REF->* TO <F_FS>.
    (Above my int tab is generated and now stored in <f_FS>)
    loop at itab into w_lifnr.
    CALL FUNCTION 'CLAF_CLASSIFICATION_OF_OBJECTS'
      EXPORTING
       CLASS                      = p_cname
       CLASSTYPE                  = w_ctype
       OBJECT                     = w_lifnr
      TABLES
        T_CLASS                    = t_class
        T_OBJECTDATA               = t_objectdata
    w-lifnr is vendor number. and here we are populating t_objectdata with that vendor related datas. now i want to pass t_objectdata-ausp1 values for each vendor across the fields in <F_FS>.
    FIELD-SYMBOLS : <F_FS> TYPE TABLE.
    Please refer me the solution to this problem.
    Thanks

    Check below code and try to add in ur code....
        IF NOT it_bom_expl[] IS INITIAL.
    The material number data varies from run to run. For this purpose
    the data is been used to build dynamic internal table which stores the
    material number data along the x-axis
          PERFORM build_matnr_table.
    The material numbers data which is stored in the temp internal table
    is been assigned to the dynamic internal table created along the
    x-axis for the materials
          PERFORM assign_matnr_val_fields.
    The component data which is stored in the internal table needs to be
    moved to the dynamic internal table
          PERFORM move_bom_data_to_matnr_table.
    *&      Form  build_matnr_table
    Using the material number data the dynamic internal tables are been
    built which holds the material number data along x-axis. Here the
    first 3 columns are always constant which are Components-List,
    Description, Count. From 4th column it depends on number of materials
    FORM build_matnr_table .
      DATA: l_val(3) TYPE n,
            lf_mat(18) TYPE c.
    Moving the Components-List details to the internal table 1st column
    which will be used for creation of dynamic internal table
      CLEAR gf_xfc.
      gf_xfc-fieldname = text-t03.
      gf_xfc-datatype = c_val_c.
      gf_xfc-inttype = c_val_c.
      gf_xfc-intlen = 18.
      gf_xfc-decimals = 0.
      APPEND gf_xfc TO gf_ifc.
      APPEND gf_xfc TO gf_ipc.
      CLEAR gf_xfc.
    Moving the Description details to the internal table 2nd column
    which will be used for creation of dynamic internal table
      gf_xfc-fieldname = text-t04.
      gf_xfc-datatype = c_val_c.
      gf_xfc-inttype = c_val_c.
      gf_xfc-intlen = 40.
      gf_xfc-decimals = 0.
      APPEND gf_xfc TO gf_ipc.
      CLEAR gf_xfc.
    Moving the Count details to the internal table 3rd column which will
    be used for creation of dynamic internal table
      gf_xfc-fieldname = text-t05.
      gf_xfc-datatype = c_val_c.
      gf_xfc-inttype = c_val_c.
      gf_xfc-intlen = 5.
      gf_xfc-decimals = 0.
      APPEND gf_xfc TO gf_ifc.
      APPEND gf_xfc TO gf_ipc.
    Moving the Material numbers are moved to the internal table from 4th
    column onwards till all the material numbers are moved to the columns
    which will be used for creation of dynamic internal table. Here
    columns will be reffered to as Material001 ..... Materialxxn
      LOOP AT it_mat.
        CLEAR gf_xfc.
        l_val = l_val + 1.
        CONCATENATE text-t06 l_val INTO lf_mat.
        gf_xfc-fieldname = lf_mat.
        gf_xfc-datatype = c_val_c.
        gf_xfc-inttype = c_val_c.
        gf_xfc-intlen = 18.
        gf_xfc-decimals = 0.
        APPEND gf_xfc TO gf_ifc.
        APPEND gf_xfc TO gf_ipc.
      ENDLOOP.
    Using the above data dynamic internal table is been created
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog = gf_ifc
        IMPORTING
          ep_table        = gf_table.
    The dynamic internal table which is created is been assigned to
    field-symbol which holds the data of the columns of X-axis
      ASSIGN gf_table->* TO <fs_dyn_table>.
    Dynamic work area is been created usng the reference to the
    field-symbol which has the data and the line item is been assign to
    field-symbol to hold line item data for reading purposes line by line
      CREATE DATA gf_line LIKE LINE OF <fs_dyn_table>.
      ASSIGN gf_line->* TO <fs_dyn_wa>.
    Using the above data dynamic internal table is been created
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog = gf_ipc
        IMPORTING
          ep_table        = gf_table1.
    The dynamic internal table which is created is been assigned to
    field-symbol which holds the data of the columns of X-axis
      ASSIGN gf_table1->* TO <fs_dyn_table1>.
    Dynamic work area is been created usng the reference to the
    field-symbol which has the data and the line item is been assign to
    field-symbol to hold line item data for reading purposes line by line
      CREATE DATA gf_line1 LIKE LINE OF <fs_dyn_table1>.
      ASSIGN gf_line1->* TO <fs_dyn_wa1>.
    Using the above data dynamic internal table is been created
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog = gf_ifc
        IMPORTING
          ep_table        = gf_table2.
    The dynamic internal table which is created is been assigned to
    field-symbol which holds the data of the columns of X-axis
      ASSIGN gf_table2->* TO <fs_dyn_table2>.
    Dynamic work area is been created usng the reference to the
    field-symbol which has the data and the line item is been assign to
    field-symbol to hold line item data for reading purposes line by line
      CREATE DATA gf_line2 LIKE LINE OF <fs_dyn_table2>.
      ASSIGN gf_line2->* TO <fs_dyn_wa2>.
    ENDFORM.                    " build_matnr_table
    *&      Form  assign_matnr_val_fields
    The material numbers data stored in the temp internal table needs to
    be assigned to the dynamic internal table along x-axis as the first
    record. The temp internal table is been looped and using the field
    symbol concept the data is moved to the dynamic internal table
    FORM assign_matnr_val_fields .
      DATA: l_cnt(3) TYPE n,
            l_nam(12),
            g_total(3) TYPE n.
      DESCRIBE TABLE it_mat LINES g_total.
      CLEAR l_cnt.
    Looping the temp internal table and concatenating material as
    material001 and the same is been checked against the dynamic
    internal table and the material number value is moved to the
    internal table. Finally, the data is been appened as first record.
      LOOP AT it_mat.
        l_nam = c_mat.
        l_cnt = l_cnt + 1.
        CONCATENATE l_nam l_cnt INTO l_nam.
    Material which is in form Materialxxn is been assigned to the field
    symbol which is checked against the field of dynamic internal table
    and the value of the Material Number is been passed to the dynamic
    internal table field value.
    After all materials are been assigned the record is been appended
    to the dynamic internal table.
        ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa> TO <fs_field>.
        <fs_field> = it_mat-matnr.
        IF l_cnt = g_total.
          INSERT <fs_dyn_wa> INTO TABLE <fs_dyn_table>.
        ENDIF.
      ENDLOOP.
    Looping the temp internal table and concatenating material as
    material001 and the same is been checked against the dynamic
    internal table and the material number value is moved to the
    internal table. Finally, the data is been appened as first record.
    This is been used for downloading of data to excel sheet as it
    contains additional field which is Description field.
      CLEAR l_cnt.
      LOOP AT it_mat.
        l_nam = c_mat.
        l_cnt = l_cnt + 1.
        CONCATENATE l_nam l_cnt INTO l_nam.
    Material which is in form Materialxxn is been assigned to the field
    symbol which is checked against the field of dynamic internal table
    and the value of the Material Number is been passed to the dynamic
    internal table field value.
    After all materials are been assigned the record is been appended
    to the dynamic internal table.
        ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa1> TO <fs_field>.
        <fs_field> = it_mat-matnr.
        IF l_cnt = g_total.
          INSERT <fs_dyn_wa1> INTO TABLE <fs_dyn_table1>.
        ENDIF.
      ENDLOOP.
    The contents of one internal table is passed on to other internal
    table for use while moving the component data for count purposes.
      <fs_dyn_table2>[] = <fs_dyn_table>[].
    ENDFORM.                    " assign_matnr_val_fields
    *&      Form  move_bom_data_to_matnr_table
    The component data is been appended to the dynamic internal table
    The component is checked against a material and if exists the corresp-
    onding level is been appended to the record
    The total count is derived as the in how many materials the component
    exists
    FORM move_bom_data_to_matnr_table .
      DATA: l_cnt(2) TYPE n,
            l_cnt1(3) TYPE n,
            l_nam(12),
            l_con(18) TYPE c,
            l_con1(18) TYPE c,
            lf_mat TYPE matnr.
      SORT it_bom_expl BY bom_comp bom_mat level.
      CLEAR: l_cnt1, <fs_dyn_wa>.
    Looping the component internal table
      LOOP AT it_bom_expl INTO gf_it_bom_expl.
        CLEAR: l_cnt1.
        AT NEW bom_comp.
          CLEAR: l_cnt, <fs_dyn_wa>, lf_mat.
    For every new bom component the material data is moved to
    temp material table which will be used for assigning the levels
    checking the count
          it_mat_temp[] = it_mat[].
    Component data is been assigned to the field symbol which is checked
    against the field of dynamic internal table and the value of the
    component number is been passed to the dynamic internal table field
    value.
          ASSIGN COMPONENT c_comp_list OF STRUCTURE <fs_dyn_wa> TO
          <fs_check>.
          <fs_check> = gf_it_bom_expl-bom_comp.
        ENDAT.
        AT NEW bom_mat.
          CLEAR l_con.
        ENDAT.
        lf_mat = gf_it_bom_expl-bom_mat.
    Looping the temp internal table and looping the dynamic internal table
    *by reading line by line into workarea, the materialxxn is been assigned
    to field symbol which will be checked and used.
        LOOP AT it_mat_temp.
          l_nam = c_mat.
          l_cnt1 = l_cnt1 + 1.
          CONCATENATE l_nam l_cnt1 INTO l_nam.
          LOOP AT <fs_dyn_table2> ASSIGNING <fs_dyn_wa2>.
            ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa2> TO <fs_xy>.
          ENDLOOP.
          IF <fs_xy> = lf_mat.
            CLEAR lf_mat.
            l_con1 = l_con.
          ENDIF.
    Checking whether the material exists for a component and if so it is
    been assigned to the field symbol which is checked against the field
    of dynamic internal table and the level of the component number
    against material is been passed to the dynamic internal table field
    value.
          IF <fs_xy> = gf_it_bom_expl-bom_mat.
            ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa> TO <fs_check>.
            CLEAR l_con.
            MOVE gf_it_bom_expl-level TO l_con.
            CONCATENATE c_val_l l_con INTO l_con.
            CONDENSE l_con NO-GAPS.
            IF l_con1 NE space.
              CONCATENATE l_con1 l_con INTO l_con SEPARATED BY c_comma.
              CLEAR l_con1.
              l_cnt = l_cnt - 1.
            ENDIF.
            <fs_check> = l_con.
            l_cnt = l_cnt + 1.
          ENDIF.
        ENDLOOP.
        AT END OF bom_comp.
    At end of every new bom component the count is moved to the field
    symbol which is checked against the field of dynamic internal table
    and the count is been passed to the dynamic internal table field
    value.
          ASSIGN COMPONENT c_count OF STRUCTURE <fs_dyn_wa> TO <fs_check>.
          <fs_check> = l_cnt.
          INSERT <fs_dyn_wa> INTO TABLE <fs_dyn_table>.
        ENDAT.
      ENDLOOP.
    Looping the component internal table. This is used for the additional
    Description field which is shown in the excel sheet
      LOOP AT it_bom_expl INTO gf_it_bom_expl.
        CLEAR: l_cnt1.
        AT NEW bom_comp.
          CLEAR: l_cnt, <fs_dyn_wa1>, lf_mat.
    For every new bom component the material data is moved to
    temp material table which will be used for assigning the levels
    checking the count
          it_mat_temp[] = it_mat[].
    Component data is been assigned to the field symbol which is checked
    against the field of dynamic internal table and the value of the
    component number is been passed to the dynamic internal table field
    value.
          ASSIGN COMPONENT c_comp_list OF STRUCTURE <fs_dyn_wa1> TO
          <fs_check>.
          <fs_check> = gf_it_bom_expl-bom_comp.
        ENDAT.
        AT NEW bom_mat.
          CLEAR l_con.
        ENDAT.
        lf_mat = gf_it_bom_expl-bom_mat.
    Looping the temp internal table and looping the dynamic internal table
    *by reading line by line into workarea, the materialxxn is been assigned
    to field symbol which will be checked and used.
        LOOP AT it_mat_temp.
          l_nam = c_mat.
          l_cnt1 = l_cnt1 + 1.
          CONCATENATE l_nam l_cnt1 INTO l_nam.
          LOOP AT <fs_dyn_table2> ASSIGNING <fs_dyn_wa2>.
            ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa2> TO <fs_xy>.
          ENDLOOP.
          IF <fs_xy> = lf_mat.
            CLEAR lf_mat.
            l_con1 = l_con.
          ENDIF.
    Checking whether the material exists for a component and if so it is
    been assigned to the field symbol which is checked against the field
    of dynamic internal table and the level of the component number
    against material is been passed to the dynamic internal table field
    value.
          IF <fs_xy> = gf_it_bom_expl-bom_mat.
            ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa1> TO <fs_check>.
            CLEAR l_con.
            MOVE gf_it_bom_expl-level TO l_con.
            CONCATENATE c_val_l l_con INTO l_con.
            CONDENSE l_con NO-GAPS.
            IF l_con1 NE space.
              CONCATENATE l_con1 l_con INTO l_con SEPARATED BY c_comma.
              CLEAR l_con1.
              l_cnt = l_cnt - 1.
            ENDIF.
            <fs_check> = l_con.
            l_cnt = l_cnt + 1.
          ENDIF.
        ENDLOOP.
    The description is moved to the field symbol which is checked against
    the field of dynamic internal table and the count is been passed to
    the dynamic internal table field value.
        ASSIGN COMPONENT c_description OF STRUCTURE <fs_dyn_wa1> TO
        <fs_check>.
        <fs_check> = gf_it_bom_expl-ojtxp.
    At end of every new bom component the count is moved to the field
    symbol which is checked against the field of dynamic internal table
    and the count is been passed to the dynamic internal table field
    value.
        AT END OF bom_comp.
          ASSIGN COMPONENT c_count OF STRUCTURE <fs_dyn_wa1> TO <fs_check>.
          <fs_check> = l_cnt.
          INSERT <fs_dyn_wa1> INTO TABLE <fs_dyn_table1>.
        ENDAT.
      ENDLOOP.
    ENDFORM.                    " move_bom_data_to_matnr_table

  • Dynamic Internal table values read and assign it back

    Hi All,
            I want to get the internal table field name dynamically and fetch its value and do some calculation and asign the new value to the same field name.
           CONCATENATE '<WA_PIPE>-ZW' v_index INTO v_fld.
            CONDENSE v_fld NO-GAPS.
            FIELD-SYMBOLS <fs> TYPE ANY.
            ASSIGN (v_fld) TO <fs>.
            v_fld1 = <fs> * <wa_pipe>-zrate.
    Now i've the internal table field name as <WA_PIPE>-ZW1 in v_fld and v_fld1 is having the value of the same field.
    How to assign it to the internal table field.
    Thanks.
    Ashok

    *& Report  YY_TEST1
    REPORT  YY_TEST1.
    type-pools: slis.
    field-symbols: <dyn_table> type standard table,
    <dyn_wa>.
    data: it_alvfc type slis_t_fieldcat_alv,
    wa_alvfc type slis_fieldcat_alv,
    it_fldcat type lvc_t_fcat,
    wa_fldcat type lvc_s_fcat.
    selection-screen begin of block b1 with frame title text-001.
      parameters: p_flds(5) type c.
    selection-screen end of block b1.
    start-of-selection.
    *build the dynamic internal table
    perform build_dyn_itab.
    *write 5 records to the alv grid
    do 5 times.
    perform build_report.
    enddo.
    *call the alv grid.
    perform call_alv.
    *Build_dyn_itab
    form build_dyn_itab.
    *Create the dynamic internal table
    data: new_table type ref to data,
    new_line type ref to data.
    *Create fields .
    do p_flds times.
    clear wa_fldcat.
    wa_fldcat-fieldname = sy-index.
    wa_fldcat-datatype = 'CHAR'.
    wa_fldcat-intlen = 5.
    append wa_fldcat to it_fldcat .
    enddo.
    *Create dynamic internal table and assign to FS
    call method cl_alv_table_create=>create_dynamic_table
    exporting
    it_fieldcatalog = it_fldcat
    importing
    ep_table = new_table.
    assign new_table->* to <dyn_table>.
    *Create dynamic work area and assign to FS
    create data new_line like line of <dyn_table>.
    assign new_line->* to <dyn_wa>.
    endform.
    *Form build_report
    form build_report.
    *Fill some values into the dynamic internal table
    data: fieldname(20) type c.
    data: fieldvalue(5) type c.
    data: index(3) type c.
    field-symbols: <fs1>.
    do p_flds times.
    index = sy-index.
    *Set up fieldvalue
    concatenate 'FLD' index into
    fieldvalue.
    condense fieldvalue no-gaps.
    assign component index of structure <dyn_wa> to <fs1>.
    <fs1> = fieldvalue.
    enddo.
    *Append to the dynamic internal table
    append <dyn_wa> to <dyn_table>.
    endform.
    *CALL_ALV
    form call_alv.
    *Build FC for ALV
    loop at it_fldcat into wa_fldcat.
    wa_alvfc-fieldname = wa_fldcat-fieldname.
    wa_alvfc-seltext_s = sy-tabix.
    wa_alvfc-outputlen = wa_fldcat-intlen.
    append wa_alvfc to it_alvfc.
    endloop.
    *Call ABAP List Viewer (ALV)
    call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
    it_fieldcat = it_alvfc
    tables
    t_outtab = <dyn_table>.
    endform.

  • Popualting data into dynamic internal table

    Hi all,
        I am working on Dynamic internal tables.I have created the structure of internal table but i am unable to populate data into it.Though i have seen some of the forums but those didn't help me. please provide me the solution. Here is my code what i did.I have the data in itab and itab1.
      My requirement is meinh fileds are dynamic.thie meinh and umren are two fileds in mvke table.for one record ihave to populate umren value for that particular meinh value.Pls help me.
    mtart     vkorg     werks     mtanr     Meinh(kg)         Meinh(gl)
    fert     0353     0303     3231     Umren value  Umren value
    REPORT  z123.
                  T Y P E - P O O L S
    TYPE-POOLS :slis,abap.
                  T A B L E S
    *Tables
    TABLES: mara,marm,mvke,marc.
                  F I E L D - S Y M B O L S
    FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
                    <dyn_wa> ,
                    <dyn_field>.
                    I N T E R N A L         T A B L E S
    Internal Tables declaration for ALV GRID/LIST
    DATA: ls_fieldcat TYPE slis_fieldcat_alv,    "FIELD CATALOG LIST
          tb_uom_cat TYPE slis_t_fieldcat_alv,   "FIELD CATALOG
          ls_fieldcat1 TYPE lvc_s_fcat,
          tb_dy_cat TYPE lvc_t_fcat,             "FIELD CATALOG
          tb_outputcat TYPE slis_t_fieldcat_alv,
          tb_events TYPE slis_t_event,
          ref_grid TYPE REF TO cl_gui_alv_grid,
          t_events LIKE LINE OF tb_events.
    DATA: BEGIN OF itab OCCURS 0,
          mtart LIKE mara-mtart,
          vkorg LIKE mvke-vkorg,
          werks LIKE marc-werks,
          matnr TYPE mara-matnr,
          END OF itab.
    DATA: BEGIN OF itab1 OCCURS 0,
          matnr TYPE marm-matnr,
          umren TYPE marm-umren,
          meinh TYPE marm-meinh,
          END OF itab1.
                            V A R I A B L E S
    Global Variables
    DATA: g_repid   LIKE sy-repid.       "Report ID
    DATA: dy_table TYPE REF TO data,
          dy_line TYPE REF TO data.
                      S E L E C T I O N   S C R E E N
    For User Input(Selection Criteria)
    SELECTION-SCREEN BEGIN OF BLOCK b0 WITH FRAME TITLE text-001.
    SELECT-OPTIONS :
      s_mtart FOR mara-mtart OBLIGATORY,    " Material type
      s_vkorg FOR mvke-vkorg OBLIGATORY,    " Sales Organization
      s_plant FOR marc-werks,               " Plant
      s_meinh FOR marm-meinh.               " Unit of Measure
    SELECTION-SCREEN END OF BLOCK b0.
                         I N I T I A L I Z A T I O N
    INITIALIZATION.
    Program ID
      g_repid = sy-repid.                      " Prog ID
                     A T   S E L E C T I O N -  S C R E E N
    AT SELECTION-SCREEN.
    Validate Material Type
      PERFORM check_mtart.
    Validate Sales Organization
      PERFORM check_vkorg.
    Validate Plant
      PERFORM check_werks.
    Validate Unit of Measure (UOM)
      PERFORM check_meinh.
                      S T A R T - O F  - S E L E C T I O N
    START-OF-SELECTION.
    Get Data
      PERFORM get_data.
    FIELD CATALOG FOR ALV
      PERFORM fill_field_catalog_uom USING tb_uom_cat.
    FIELD CATALOG FOR CREATING DYNAMIC INTERNAL TABLE
      PERFORM fill_field_catalog_dy USING tb_dy_cat.
    CREATING DYNAMIC INTERNAL TABLE
      PERFORM dy_tab_create.
                       End of selection                                  *
                              F O R M S
    *&      Form  CHECK_MTART
          Validate Material Type
    FORM check_mtart .
    Material Type
      DATA :l_mtart LIKE t134-mtart.
      CHECK NOT s_mtart IS INITIAL.
      SELECT mtart UP TO 1 ROWS
        INTO l_mtart
        FROM t134
       WHERE mtart IN s_mtart.
      ENDSELECT.
      IF sy-subrc <> 0.
        MESSAGE e899(mm) WITH 'Invalid Material Type'(002).
      ENDIF.
    ENDFORM.                    " CHECK_MTART
    *&      Form  CHECK_VKORG
          Validate Sales Organization
    FORM check_vkorg .
    Sales Oraganization
      DATA :l_vkorg LIKE tvko-vkorg.
      CHECK NOT s_vkorg IS INITIAL.
      SELECT vkorg UP TO 1 ROWS
        INTO l_vkorg
        FROM tvko
       WHERE vkorg IN s_vkorg.
      ENDSELECT.
      IF sy-subrc <> 0.
        MESSAGE e899(mm) WITH 'Invalid Sales Organization'(003).
      ENDIF.
    ENDFORM.                    " CHECK_VKORG
    *&      Form  CHECK_WERKS
          Validate Plant
    FORM check_werks .
    Plant
      DATA :l_werks LIKE t001w-werks.
      CHECK NOT s_plant IS INITIAL.
      SELECT werks UP TO 1 ROWS
        INTO l_werks
        FROM t001w
       WHERE werks IN s_plant.
      ENDSELECT.
      IF sy-subrc <> 0.
        MESSAGE e899(mm) WITH 'Invalid Plant'(004).
      ENDIF.
    ENDFORM.                    " CHECK_WERKS
    *&      Form  CHECK_MEINH
          Validate Unit of Measure (UOM)
    FORM check_meinh .
    Unit of Measure (UOM)
      DATA :l_meinh LIKE t006-msehi.
      CHECK NOT s_meinh IS INITIAL.
      SELECT msehi UP TO 1 ROWS
        INTO l_meinh
        FROM t006
        WHERE msehi IN s_meinh.
      ENDSELECT.
      IF sy-subrc <> 0.
        MESSAGE e899(mm) WITH 'Invalid Unit of Measure'(005).
      ENDIF.
    ENDFORM.                    " CHECK_MEINH
    *&      Form  FILL_FIELD_CATALOG_UOM
          text
         FIELD CATALOG FOR ALV
    FORM fill_field_catalog_uom  USING l_fieldcat TYPE  slis_t_fieldcat_alv.
      DATA: BEGIN OF l_itab OCCURS 0,
            meinh TYPE marm-meinh,
            END OF l_itab.
    DATA: l_count TYPE i.
      DATA: l_fieldcat_col_pos TYPE i.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname   = 'MTART'.
      ls_fieldcat-key         = ''.
      ls_fieldcat-col_pos     = 1.
      ls_fieldcat-ref_fieldname = 'MTART'.
      ls_fieldcat-ref_tabname   = 'MARA'.
      APPEND ls_fieldcat TO l_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname   = 'VKORG'.
      ls_fieldcat-key         = ''.
      ls_fieldcat-col_pos     = 2.
      ls_fieldcat-ref_fieldname = 'VKORG'.
      ls_fieldcat-ref_tabname   = 'MVKE'.
      APPEND ls_fieldcat TO l_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname   = 'WERKS'.
      ls_fieldcat-key         = ''.
      ls_fieldcat-col_pos     = 3.
      ls_fieldcat-ref_fieldname = 'WERKS'.
      ls_fieldcat-ref_tabname   = 'MARC'.
      APPEND ls_fieldcat TO l_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname   = 'MATNR'.
      ls_fieldcat-key         = 'x'.
      ls_fieldcat-col_pos     = 4.
      ls_fieldcat-ref_fieldname = 'MATNR'.
      ls_fieldcat-ref_tabname   = 'MARA'.
      APPEND ls_fieldcat TO l_fieldcat.
      CLEAR ls_fieldcat.
      l_fieldcat_col_pos = 5.
    Getting the UOM as per user selection
      SELECT  msehi  FROM t006
        INTO TABLE l_itab
        WHERE msehi IN s_meinh.
      LOOP AT l_itab.
        ls_fieldcat-fieldname   = l_itab-meinh.
        ls_fieldcat-key         = ''.
        ls_fieldcat-col_pos     = l_fieldcat_col_pos.
        ls_fieldcat-ref_fieldname = 'MSEHI'.
        ls_fieldcat-ref_tabname   = 'T006'.
        APPEND ls_fieldcat TO l_fieldcat.
        CLEAR ls_fieldcat.
        l_fieldcat_col_pos = l_fieldcat_col_pos + 1.
      ENDLOOP.
    ENDFORM.                    " FILL_FIELD_CATALOG_UOM
    *&      Form  DISPLAY_DATA
          text
    -->  p1        text
    <--  p2        text
    FORM display_data .
    ENDFORM.                    " DISPLAY_DATAENDFORM.                    " FILL_FIELD_CATALOG_UOM
    *&      Form  DY_TAB_CREATE
          text
    -->  p1        text
    <--  p2        text
    FORM dy_tab_create .
    CREATE DYNAMIC INTERNAL TABLE AND ASSIGN TO FS
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog = tb_dy_cat
        IMPORTING
          ep_table        = dy_table.
      ASSIGN dy_table->* TO <dyn_table>.
    *CREATE DYNAMIC WORK AREA AND AASIGN TO FS
      CREATE DATA dy_line LIKE LINE OF  <dyn_table>.
      ASSIGN dy_line->* TO <dyn_wa>.
    ENDFORM.                    " DY_TAB_CREATE
    *&      Form  FILL_FIELD_CATALOG_DY
          text
        FIELD CATALOG FOR CREATING A DYNAMIC TABLE
    FORM fill_field_catalog_dy  USING  l_fieldcat TYPE lvc_t_fcat.
      DATA: BEGIN OF l_itab OCCURS 0,
           meinh TYPE marm-meinh,
           END OF l_itab.
      DATA: l_count TYPE i.
      DATA: l_fieldcat_col_pos TYPE i.
      DATA: l_umren TYPE string.
      DATA: l_var TYPE string.
      CLEAR ls_fieldcat.
      ls_fieldcat1-fieldname   = 'MTART'.
      ls_fieldcat1-key         = ''.
      ls_fieldcat1-col_pos     = 1.
      ls_fieldcat1-datatype = 'char'.
      ls_fieldcat1-intlen = 4.
      APPEND ls_fieldcat1 TO l_fieldcat.
      CLEAR ls_fieldcat1.
      ls_fieldcat1-fieldname   = 'VKORG'.
      ls_fieldcat1-key         = ''.
      ls_fieldcat1-col_pos     = 2.
      ls_fieldcat1-datatype = 'char'.
      ls_fieldcat1-intlen = 4.
      APPEND ls_fieldcat1 TO l_fieldcat.
      CLEAR ls_fieldcat1.
      ls_fieldcat1-fieldname   = 'WERKS'.
      ls_fieldcat1-key         = ''.
      ls_fieldcat1-col_pos     = 3.
      ls_fieldcat1-datatype = 'char'.
      ls_fieldcat1-intlen = 4.
      APPEND ls_fieldcat1 TO l_fieldcat.
      CLEAR ls_fieldcat1.
      ls_fieldcat1-fieldname   = 'MATNR'.
      ls_fieldcat1-key         = 'x'.
      ls_fieldcat1-col_pos     = 4.
      ls_fieldcat1-datatype = 'char'.
      ls_fieldcat1-intlen = 18.
      APPEND ls_fieldcat1 TO l_fieldcat.
      CLEAR ls_fieldcat1.
      l_fieldcat_col_pos = 5.
    Getting the UOM as per user selection
      SELECT  msehi  FROM t006
        INTO TABLE l_itab
        WHERE msehi IN s_meinh.
      SORT l_itab ASCENDING.
      DESCRIBE TABLE l_itab LINES l_count.
      LOOP AT l_itab.
        ls_fieldcat1-fieldname   = l_itab-meinh.
        ls_fieldcat1-key         = ''.
        ls_fieldcat1-col_pos     = l_fieldcat_col_pos.
        ls_fieldcat1-datatype = 'dec'.
        ls_fieldcat1-intlen = 5.
        APPEND ls_fieldcat1 TO l_fieldcat.
        CLEAR ls_fieldcat1.
        l_fieldcat_col_pos = l_fieldcat_col_pos + 1.
      ENDLOOP.
    ENDFORM.                    " FILL_FIELD_CATALOG_DY
    *&      Form  GET_DATA
          text
    -->  p1        text
    <--  p2        text
    FORM get_data .
      SELECT
      amtart bvkorg cwerks amatnr
      INTO TABLE itab
      FROM mara AS a
      JOIN mvke AS b ON  bmatnr = amatnr
      JOIN marc AS c ON  cmatnr = bmatnr
      JOIN marm AS d ON  dmatnr = cmatnr
      WHERE a~mtart IN s_mtart
       AND  b~vkorg IN s_vkorg
       AND  c~werks IN s_plant.
      SORT itab ASCENDING.
      DELETE ADJACENT DUPLICATES FROM itab.
      SELECT matnr umren meinh FROM marm
      APPENDING TABLE itab1
      FOR ALL ENTRIES IN itab
      WHERE matnr = itab-matnr
      AND meinh IN s_meinh .
    ENDFORM.                    " GET_DATA

    can you check this for creation of dynamic table
    ******DATA DECLARATION*****************************
    FIELD-SYMBOLS : <it_final> TYPE STANDARD TABLE,
                    <wa_final> TYPE ANY,
                    <w_field> TYPE ANY.
    ***DYNAMIC CREATION OF FIELDCATALOG****************
    *FIRST 2 FIELDS FIELDS FIELD1 AND FIELD2 ARE CONSTANT, FIELDS OBTAINED IN THE LOOP ENDLOOP ARE DYNAMIC,
    *LIKEWISE DYNAMIC FIELDCATALOG IS CREATED
      wa_fieldcatalog-fieldname  = 'FIELD1'.
      wa_fieldcatalog-ref_table  = 'E070'.
      wa_fieldcatalog-outputlen  = '13'.
      wa_fieldcatalog-reptext    = 'Created On'.
      wa_fieldcatalog-seltext    = 'Created On'.
      APPEND wa_fieldcatalog TO it_fieldcatalog.
      CLEAR wa_fieldcatalog.
      wa_fieldcatalog-fieldname  = 'FIELD1'.
      wa_fieldcatalog-ref_table  = 'E070'.
      wa_fieldcatalog-outputlen  = '13'.
      wa_fieldcatalog-reptext    = 'Created On'.
      wa_fieldcatalog-seltext    = 'Created On'.
      APPEND wa_fieldcatalog TO it_fieldcatalog.
      CLEAR wa_fieldcatalog.
      LOOP AT it_mandt WHERE mandt IN s_mandt.
        CONCATENATE 'CLNT' it_mandt INTO wa_fieldcatalog-fieldname.
        wa_fieldcatalog-inttype    = 'NUMC'.
        wa_fieldcatalog-outputlen  = '14'.
        wa_fieldcatalog-reptext    = it_mandt.
        wa_fieldcatalog-seltext    = it_mandt.
        APPEND wa_fieldcatalog TO it_fieldcatalog.
        CLEAR :wa_fieldcatalog ,it_mandt.
      ENDLOOP.
    ********CREATE DYNAMIC TABLE************************
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog           = it_fieldcatalog
        IMPORTING
          ep_table                  = new_table
        EXCEPTIONS
          generate_subpool_dir_full = 1
          OTHERS                    = 2.
      IF sy-subrc <> 0.
    *  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
      ASSIGN new_table->* TO <it_final>.
    *********CREATE WORK AREA****************************
    CREATE DATA new_line LIKE LINE OF <it_final>.
      ASSIGN new_line->* TO <wa_final>.
    *********INSERTTING WORK AREAR TO INTERNAL TABLE******
        INSERT <wa_final> INTO TABLE <it_final>.
    *******POPULATING DATA******************************* 
      LOOP.
       ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_final> TO <w_field>.
       <w_field> = '12345'.
        ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_final> TO <w_field>.
       <w_field> = '21453DD'.
       FIELD1 AND FIELD2 ARE COMPONENTS OF FIELDCATALOG.
    ENDLOOP.     
      ENDLOOP.

  • Column to row display of internal table values

    Hi guys,
    I have the internal table in the following manner
    MATNR     ADSIZE     PRICE
    220p     15m     100
    220p     16m     120
    220p     17m     230
    230p     16m     200
    230p     17m     120
    230p     18m     220
    240p     17m     120
    250p     18m     560
    And want to change to the below mentioned manner, please note that there can be multiple matnr's and multiple adsizes as well.
    MATNR     15m     16m     17m     18m     ......(dynamic:depends on                                                                               
    user )
    220p     100     120     230
    230p     -     200     120     220...and so on.
    How do i display in the above manner.
    Please help.
    Thanks in advance.
    Regards,
    Santosh Kotra.

    Hi Santosh.
    I work hard in this topic. I will post my solution, try to adequate to your needs.
    If your problem is solved, talk to me about your solution.
    I create 2 dynamics tables to do this. I create a it to save the column position to populate the table for alv.
    Change the select (form select) to see with others filters.
    I hope this help you and others.
    Regards
    *& Report  ZDYNAMIC                                                    *
    REPORT  zdynamic                                .
    Tables
    DATA: lt_data TYPE REF TO data,
          lt_new  TYPE REF TO data.
    DATA: lt_fieldcatalog TYPE lvc_t_fcat.
    Structure
    DATA: ls_fieldcatalog TYPE lvc_s_fcat,
          BEGIN OF mat OCCURS 0,
            name TYPE string,
            pos TYPE i,
          END OF mat.
    Data References
    DATA: new_line TYPE REF TO data,
          tab_line TYPE REF TO data.
    Field Symbols
    FIELD-SYMBOLS: <fs_data> TYPE REF TO data,
                   <fs_data1> TYPE REF TO data,
                   <fs_1> TYPE STANDARD TABLE,
                   <ntab> TYPE STANDARD TABLE,
                   <fs_2>,
                   <tab2>,
                   <fs_3>,
                   <tab3>.
    Vars
    DATA: index LIKE sy-index.
    PERFORM fieldcat.
    ASSIGN lt_data TO <fs_data>.
    ASSIGN lt_new  TO <fs_data1>.
    PERFORM cria_tab USING lt_fieldcatalog 1.
    ASSIGN <fs_data>->* TO <fs_1>.
    CREATE DATA new_line LIKE LINE OF <fs_1>.
    ASSIGN new_line->*  TO <fs_2>.
    And to put the data in the internal table
    PERFORM select.
    preencher fieldcat tabela nova
    CLEAR lt_fieldcatalog[].
    LOOP AT <fs_1> ASSIGNING <fs_2>.
      DO.
        ASSIGN COMPONENT sy-index OF STRUCTURE <fs_2> TO <fs_3>.
        IF sy-subrc <> 0.
          EXIT.
        ENDIF.
        IF sy-index = 1.
          CLEAR ls_fieldcatalog.
          ls_fieldcatalog-coltext   = 'CARRID'.
          ls_fieldcatalog-fieldname = 'CARRID'.
          ls_fieldcatalog-inttype = 'C'.
          APPEND ls_fieldcatalog TO lt_fieldcatalog.
        ENDIF.
        IF sy-index = 2.
          CLEAR ls_fieldcatalog.
          ls_fieldcatalog-coltext   = <fs_3>.
          ls_fieldcatalog-fieldname = <fs_3>.
          ls_fieldcatalog-inttype = 'C'.
          APPEND ls_fieldcatalog TO lt_fieldcatalog.
        ENDIF.
      ENDDO.
    ENDLOOP.
    SORT lt_fieldcatalog DESCENDING BY fieldname.
    DELETE ADJACENT DUPLICATES FROM lt_fieldcatalog.
    LOOP AT lt_fieldcatalog INTO ls_fieldcatalog.
      mat-name = ls_fieldcatalog-coltext.
      mat-pos  = sy-tabix.
      APPEND mat.
    ENDLOOP.
    *create new table
    PERFORM cria_tab USING lt_fieldcatalog 2.
    add data
    ASSIGN <fs_data1>->* TO <ntab>.
    CREATE DATA tab_line LIKE LINE OF <ntab>.
    ASSIGN tab_line->*  TO <tab2>.
    LOOP AT <fs_1> ASSIGNING <fs_2>.
      DO.
        ASSIGN COMPONENT sy-index OF STRUCTURE <fs_2> TO <fs_3>.
        IF sy-subrc <> 0.
          EXIT.
        ENDIF.
        IF sy-index = 1.
          ASSIGN COMPONENT sy-index OF STRUCTURE <tab2> TO <tab3>.
          <tab3> = <fs_3>.
        ELSEIF sy-index = 3.
          ASSIGN COMPONENT 2 OF STRUCTURE <fs_2> TO <fs_3>.
          READ TABLE mat WITH KEY name = <fs_3>.
          index = mat-pos.
          ASSIGN COMPONENT sy-index OF STRUCTURE <fs_2> TO <fs_3>.
          ASSIGN COMPONENT index OF STRUCTURE <tab2> TO <tab3>.
          <tab3> = <fs_3>.
        ENDIF.
      ENDDO.
      APPEND <tab2> TO <ntab>.
      CLEAR <tab2>.
    ENDLOOP.
    PERFORM display.
    FORM display
    FORM display.
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
        EXPORTING
          it_fieldcat_lvc = lt_fieldcatalog
        TABLES
         t_outtab        = <fs_1>
          t_outtab        = <ntab>
        EXCEPTIONS
          program_error   = 1
          OTHERS          = 2.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
    ENDFORM.                    "display
    FORM fieldcat
    FORM fieldcat.
      CLEAR ls_fieldcatalog.
      ls_fieldcatalog-coltext   = 'CARRID'.
      ls_fieldcatalog-fieldname = 'CARRID'. "Fieldname
      ls_fieldcatalog-inttype = 'C'. "Internal Type C-> Character
      APPEND ls_fieldcatalog TO lt_fieldcatalog.
      ls_fieldcatalog-coltext   = 'CONNID'.
      ls_fieldcatalog-fieldname = 'CONNID'.
      ls_fieldcatalog-inttype = 'N'.
      APPEND ls_fieldcatalog TO lt_fieldcatalog.
      ls_fieldcatalog-coltext   = 'FLDATE'.
      ls_fieldcatalog-fieldname = 'FLDATE'.
      ls_fieldcatalog-inttype = 'D'.
      APPEND ls_fieldcatalog TO lt_fieldcatalog.
      ls_fieldcatalog-coltext   = 'PRICE'.
      ls_fieldcatalog-fieldname = 'PRICE'.
      ls_fieldcatalog-inttype = 'P'.
      APPEND ls_fieldcatalog TO lt_fieldcatalog.
      ls_fieldcatalog-coltext   = 'CURRENCY'.
      ls_fieldcatalog-fieldname = 'CURRENCY'.
      ls_fieldcatalog-inttype = 'C'.
      APPEND ls_fieldcatalog TO lt_fieldcatalog.
    ENDFORM.                    "fieldcat
    FORM cria_tab
    FORM cria_tab USING catalog TYPE lvc_t_fcat qual TYPE i.
      IF qual = 1.
        CALL METHOD cl_alv_table_create=>create_dynamic_table
          EXPORTING
            it_fieldcatalog           = catalog
          IMPORTING
            ep_table                  = <fs_data>
          EXCEPTIONS
            generate_subpool_dir_full = 1
            OTHERS                    = 2.
      ELSE.
        CALL METHOD cl_alv_table_create=>create_dynamic_table
          EXPORTING
            it_fieldcatalog           = catalog
          IMPORTING
            ep_table                  = <fs_data1>
          EXCEPTIONS
            generate_subpool_dir_full = 1
            OTHERS                    = 2.
      ENDIF.
    ENDFORM.                    "cria_tab
    FORM select
    FORM select.
      SELECT mandt carrid connid fldate price currency
        FROM sflight
        INTO CORRESPONDING FIELDS OF TABLE <fs_1>
        WHERE "carrid = 'AA' AND
        fldate > '20080101'.
    ENDFORM.                    "select

  • How do I loop through tables, not columns in a table?

    Sorry if this is a long one. My problem is actually quite simple. I am trying to write either an ad hoc PL/SQL block or a stored procedure to loop 18 times (thru 18 tables) and perform a SQL query on those tables, returning 18 resultsets. I would like the results to show up on the screen (or in a spool file, whatever).
    So far I have tried 3 different approaches, none of which have worked.
    1. I tried to assign the select query to a variable (qry) and use EXECUTE IMMEDIATE qry. This forced me thru a variety of errors to declare variables and assign the result to them--EXECUTE IMMEDIATE qry into nm0, nm1, nm2...
    The problem with that was the resultset returned more than the variable was built for as there might be no rows returned, or it might be a thousand rows of data. So I tried changing the variables to VARRAYS, but it gave me a type mismatch as the underlying columns were NUMBER and VARCHAR2.
    DECLARE
         ctr number;
         TYPE NUMLIST IS VARRAY (1000) OF NUMBER;
         TYPE VARLIST IS VARRAY (1000) OF VARCHAR2(15);
         nm0 NUMLIST NOT NULL DEFAULT 1;
         nm1 VARLIST;
         nm2 NUMLIST NOT NULL DEFAULT 1;
         nm3 VARLIST;
         nm17 NUMLIST NOT NULL DEFAULT 1;
         qry VARCHAR2(2000) := 'klx_uln_p000_cells';
    BEGIN
    FOR ctr IN 1..17 LOOP
         IF ctr &lt; 10 THEN
              qry := 'SELECT
              A.DIM_0_INDEX,
              S0.SYM_NAME,
              A.DIM_1_INDEX,
              S1.SYM_NAME,
              A.DIM_2_INDEX,
              S2.SYM_NAME,
              A.DIM_3_INDEX,
              S3.SYM_NAME,
              A.NUMERIC_VALUE,
              B.NUMERIC_VALUE
              FROM
              KHALIX.klx_uln_p00'||ctr||'_cells A,
              KHALIX.klx_ucn_p00'||ctr||'_cells B,
              KHALIX.KLX_MASTER_SYMBOL S0,
              KHALIX.KLX_MASTER_SYMBOL S1,
              KHALIX.KLX_MASTER_SYMBOL S7
              WHERE
              A.DIM_0_INDEX=B.DIM_0_INDEX AND
              A.DIM_1_INDEX=B.DIM_1_INDEX AND...
              A.DIM_7_INDEX=S7.SYM_INDEX';
         ELSE
              qry := 'SELECT
              A.DIM_0_INDEX...
              A.DIM_7_INDEX=S7.SYM_INDEX';
         END IF;
         BEGIN
         dbms_output.put_line('SELECT FOR KLX_ULN_P00'||ctr||'_CELLS');
         dbms_output.put_line(nm16);
         dbms_output.put_line(nm17);
         EXECUTE IMMEDIATE qry into nm0,nm1,nm2,nm3,nm4,nm5,nm6,nm7,nm8,nm9,nm10,nm11,nm12,nm13,nm14,nm15,nm16,nm17;
    --     dbms_output.put_line(qry);
         dbms_output.put_line(nm16);
         dbms_output.put_line(nm17);
         EXCEPTION
              WHEN NO_DATA_FOUND THEN
                   dbms_output.put_line('No data found for Query '||ctr);
         END;
    END LOOP;
    EXCEPTION
         WHEN NO_DATA_FOUND THEN
              dbms_output.put_line('No data found for Query '||ctr);
    END;
    2. So then I used REF CURSOR to create a stored procedure and return the values. That allowed me to run my query AND tokenize the tablenames with a counter so that it would loop through the different tables! However, I still could not get it to display the results without going to SQL Plus and typing 'print c;'.
    3. So, finally I tried to create a looping wrapper around the ref cursor to have some variable (ctr) increment so my query would get performed on table0_cells through table17_cells. This, too, did not work.
    If I manually go to SQL Plus and type:
    variable ctr number
    begin
    :ctr := 1;
    end;
    exec dupe_find(1,:c);
    it will execute for the first table (klx_uln_p001_cells) and I can then type 'print c' to see what was returned. But when I try putting this within a wrapper PL/SQL block with a Loop to make ctr go from 0 - 17 (to loop through table_names klx_uln_p000_cells to klx_uln_p017_cells), it does not work.
    Help! It should be very simple to loop through tables, shouldn't it? I just want a script that will loop through tables, perform a query on each table and display the results. For some reason, I can only find documentation examples on looping through columns that are all in the same table.
    Dave

    Here's a working example using your first strategy ...
    create table t1 (id number);
    create table t2 (id number);
    insert into t1 values (100);
    insert into t1 values (101);
    insert into t2 values (200);
    insert into t2 values (201);
    declare
    v_table_name user_tables.table_name%type;
    type ttab_id is table of t1.id%type index by binary_integer;
    tab_id ttab_id;
    begin
    for i in 1 .. 2 loop
    v_table_name := 't' || i;
    execute immediate 'select id from ' || v_table_name
    bulk collect into tab_id;
    dbms_output.put_line('query from ' || v_table_name);
    for j in 1 .. tab_id.count loop
    dbms_output.put_line(tab_id(j));
    end loop;
    end loop;
    end;
    There are many other ways to do this (especially if you need to do more than just print out the data).
    Richard

  • Internal table with Dynamic and Non dynamic fileds

    Hi Experts,
    How to get the internal table with Dynamic and Non-Dynamic Fields.
    Could u please help me.
    Thanks,
    Varun

    Hi,
       Execute the below sample code or analyze it there is appropriate description provided.
    *& Report  ZTEST_PRM_DYN_ALV
    REPORT  ZTEST_PRM_DYN_ALV.
    type-pools: slis.
    field-symbols: <dyn_table> type standard table,
    <dyn_wa>.
    data: alv_fldcat type slis_t_fieldcat_alv,
    it_fldcat type lvc_t_fcat.
    selection-screen begin of block b1 with frame title text-001.
    parameters: p_flds(5) type c.
    selection-screen end of block b1.
    start-of-selection.
    *build the dynamic internal table
    perform build_dyn_itab.
    *write 5 records to the alv grid
    do 5 times.
    perform build_report.
    enddo.
    *call the alv grid.
    perform call_alv.
    *Build_dyn_itab
    form build_dyn_itab.
    data: new_table type ref to data,
    new_line type ref to data,
    wa_it_fldcat type lvc_s_fcat.
    *Create fields .
    clear wa_it_fldcat.
    wa_it_fldcat-fieldname = 'name1'.
    wa_it_fldcat-datatype = 'mara-matnr'.
    wa_it_fldcat-intlen = 5.
    append wa_it_fldcat to it_fldcat .
    *clear wa_it_fldcat.
    wa_it_fldcat-fieldname = sy-index.
    wa_it_fldcat-datatype = 'CHAR'.
    wa_it_fldcat-intlen = 5.
    append wa_it_fldcat to it_fldcat .
    do p_flds times.
    clear wa_it_fldcat.
    wa_it_fldcat-fieldname = sy-index.
    wa_it_fldcat-datatype = 'CHAR'.
    wa_it_fldcat-intlen = 6.
    append wa_it_fldcat to it_fldcat .
    enddo.
    *Create dynamic internal table and assign to FS
    call method cl_alv_table_create=>create_dynamic_table
    exporting
    it_fieldcatalog = it_fldcat
    importing
    ep_table = new_table.
    assign new_table->* to <dyn_table>.
    *Create dynamic work area and assign to FS
    create data new_line like line of <dyn_table>.
    assign new_line->* to <dyn_wa>.
    endform.
    *Form build_report
    form build_report.
    data: fieldname(20) type c.
    data: fieldvalue(5) type c.
    data: index(3) type c.
    field-symbols: <fs1>.
    do p_flds times.
    index = sy-index.
    *Set up fieldvalue
    concatenate 'FLD' index into
    fieldvalue.
    condense fieldvalue no-gaps.
    assign component index of structure <dyn_wa> to <fs1>.
    <fs1> = fieldvalue.
    enddo.
    *Append to the dynamic internal table
    append <dyn_wa> to <dyn_table>.
    endform.
    *CALL_ALV
    form call_alv.
    data: wa_cat like line of alv_fldcat.
    *clear wa_cat.
    wa_cat-fieldname = 'matnr'.
    wa_cat-seltext_s = sy-index.
    wa_cat-outputlen = '10'.
    append wa_cat to alv_fldcat.
    do p_flds times.
    clear wa_cat.
    wa_cat-fieldname = sy-index.
    wa_cat-seltext_s = sy-index.
    wa_cat-outputlen = '6'.
    append wa_cat to alv_fldcat.
    enddo.
    *Call ABAP List Viewer (ALV)
    call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
    it_fieldcat = alv_fldcat
    tables
    t_outtab = <dyn_table>.
    endform.
    Hope this will help, reward if found usefull.
    Cheers,
    Ram.

  • Filling Data in Dynamic internal table

    Hello,
    I have 2 internal tables TAB1 and TAB2.
    I have Created Dynamic internal table From TAB1 rows.
    Now I want to fill that Dynm. internal table from TAB2.
    But TAB2  have more Rows with diffarant  names.  I want to move particular field of TAB2 to particular field.of dynamic IT.
    kindly help.

    Hi,
    I am sending the dynamic alv report for your referenece.
    REPORT  YMS_DYNAMICALV.
    type-pools: slis.
    field-symbols: <dyn_table> type standard table,
                   <dyn_wa>.
    data: alv_fldcat type slis_t_fieldcat_alv,
          it_fldcat type lvc_t_fcat.
    selection-screen begin of block b1 with frame title text-001.
    parameters: p_flds(5) type c.
    selection-screen end of block b1.
    start-of-selection.
    build the dynamic internal table
      perform build_dyn_itab.
    write 5 records to the alv grid
      do 5 times.
        perform build_report.
      enddo.
    call the alv grid.
      perform call_alv.
    Build_dyn_itab
    form build_dyn_itab.
      data: new_table type ref to data,
            new_line  type ref to data,
            wa_it_fldcat type lvc_s_fcat.
    Create fields .
      do p_flds times.
        clear wa_it_fldcat.
        wa_it_fldcat-fieldname = sy-index.
        wa_it_fldcat-datatype = 'CHAR'.
        wa_it_fldcat-intlen = 5.
        append wa_it_fldcat to it_fldcat .
      enddo.
    Create dynamic internal table and assign to FS
      call method cl_alv_table_create=>create_dynamic_table
                   exporting
                      it_fieldcatalog = it_fldcat
                   importing
                      ep_table        = new_table.
      assign new_table->* to <dyn_table>.
    Create dynamic work area and assign to FS
      create data new_line like line of <dyn_table>.
      assign new_line->* to <dyn_wa>.
    endform.
         Form  build_report
    form build_report.
      data: fieldname(20) type c.
      data: fieldvalue(5) type c.
      data: index(3) type c.
      field-symbols: <fs1>.
      do p_flds times.
        index = sy-index.
    Set up fieldvalue
        concatenate 'FLD' index into
                 fieldvalue.
        condense   fieldvalue no-gaps.
      <b> assign component  index  of structure <dyn_wa> to <fs1>.
       <fs1> =  fieldvalue.</b>
      enddo.
    Append to the dynamic internal table
      append <dyn_wa> to <dyn_table>.
    endform.
    CALL_ALV
    form call_alv.
      data: wa_cat like line of alv_fldcat.
      do p_flds times.
        clear wa_cat.
        wa_cat-fieldname = sy-index.
        wa_cat-seltext_s = sy-index.
        wa_cat-outputlen = '5'.
        append wa_cat to alv_fldcat.
      enddo.
    Call ABAP List Viewer (ALV)
      call function 'REUSE_ALV_GRID_DISPLAY'
           exporting
                it_fieldcat = alv_fldcat
           tables
                t_outtab    = <dyn_table>.
    endform.
    Thanks,
    Shankar

  • Creating Dynamic internal table

    Hi,
    Can we create a dynamic internal table using data of an existing internal table.
    i.e. if itab1 is have data like :
    row                  col                     value
    1                      1                        ab
    1                      2                        cd
    1                      3                        ef
    2                      1                        gh
    2                      2                        ij
    2                      3                        kl
    and I want to create itab2 dynamically like this :
    row                  col1                     col2                    col3
    1                       ab                        cd                       ef
    2                       gh                          ij                        kl
    The issue lies only while creating internal table using non DDIC structure i.e. internal table of program.
    Thanks
    Vipin

    Hi,
    This is the sample report for DYNAMIC ALV report.
    REPORT  YMS_DYNAMICALV.
    TYPE-POOLS: SLIS.
    FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
                   <DYN_WA>.
    DATA: ALV_FLDCAT TYPE SLIS_T_FIELDCAT_ALV,
          IT_FLDCAT TYPE LVC_T_FCAT.
    SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
    PARAMETERS: P_FLDS(5) TYPE C.
    SELECTION-SCREEN END OF BLOCK B1.
    START-OF-SELECTION.
    build the dynamic internal table
      PERFORM BUILD_DYN_ITAB.
    write 5 records to the alv grid
      DO 5 TIMES.
        PERFORM BUILD_REPORT.
      ENDDO.
    call the alv grid.
      PERFORM CALL_ALV.
    Build_dyn_itab
    FORM BUILD_DYN_ITAB.
      DATA: NEW_TABLE TYPE REF TO DATA,
            NEW_LINE  TYPE REF TO DATA,
            WA_IT_FLDCAT TYPE LVC_S_FCAT.
    Create fields .
      DO P_FLDS TIMES.
        CLEAR WA_IT_FLDCAT.
        WA_IT_FLDCAT-FIELDNAME = SY-INDEX.
        WA_IT_FLDCAT-DATATYPE = 'CHAR'.
        WA_IT_FLDCAT-INTLEN = 5.
        APPEND WA_IT_FLDCAT TO IT_FLDCAT .
      ENDDO.
    Create dynamic internal table and assign to FS
      CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
        EXPORTING
          IT_FIELDCATALOG = IT_FLDCAT
        IMPORTING
          EP_TABLE        = NEW_TABLE.
      ASSIGN NEW_TABLE->* TO <DYN_TABLE>.
    Create dynamic work area and assign to FS
      CREATE DATA NEW_LINE LIKE LINE OF <DYN_TABLE>.
      ASSIGN NEW_LINE->* TO <DYN_WA>.
    ENDFORM.                    "build_dyn_itab
         Form  build_report
    FORM BUILD_REPORT.
      DATA: FIELDNAME(20) TYPE C.
      DATA: FIELDVALUE(5) TYPE C.
      DATA: INDEX(3) TYPE C.
      FIELD-SYMBOLS: <FS1>.
      DO P_FLDS TIMES.
        INDEX = SY-INDEX.
    Set up fieldvalue
        CONCATENATE 'FLD' INDEX INTO
                 FIELDVALUE.
        CONDENSE   FIELDVALUE NO-GAPS.
      <b> assign component  index  of structure <dyn_wa> to <fs1>.
       <fs1> =  fieldvalue.</b>
      ENDDO.
    Append to the dynamic internal table
      APPEND <DYN_WA> TO <DYN_TABLE>.
    ENDFORM.                    "build_report
    CALL_ALV
    FORM CALL_ALV.
      DATA: WA_CAT LIKE LINE OF ALV_FLDCAT.
      DO P_FLDS TIMES.
        CLEAR WA_CAT.
        WA_CAT-FIELDNAME = SY-INDEX.
        WA_CAT-SELTEXT_S = SY-INDEX.
        WA_CAT-OUTPUTLEN = '5'.
        APPEND WA_CAT TO ALV_FLDCAT.
      ENDDO.
    Call ABAP List Viewer (ALV)
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          IT_FIELDCAT = ALV_FLDCAT
        TABLES
          T_OUTTAB    = <DYN_TABLE>.
    ENDFORM.                    "call_alv
    Thanks,
    Sankar M

  • Dynamic internal table for ALV output

    Hi,
    I am looking for a simple & efficient sample code for an ALV report output with dynamic columns. Can someone please help!
    Thanks,
    JS

    Here is a small example. This is pretty old code, and it uses the FM approach.
    report zrich_0001.
    type-pools: slis.
    field-symbols: <dyn_table> type standard table,
                   <dyn_wa>.
    data: it_alvfc type slis_t_fieldcat_alv,
          wa_alvfc type slis_fieldcat_alv,
          it_fldcat type lvc_t_fcat,
          wa_fldcat type lvc_s_fcat.
    selection-screen begin of block b1 with frame title text-001.
    parameters: p_flds(5) type c.
    selection-screen end of block b1.
    start-of-selection.
    * build the dynamic internal table
      perform build_dyn_itab.
    * write 5 records to the alv grid
      do 5 times.
        perform build_report.
      enddo.
    * call the alv grid.
      perform call_alv.
    *  Build_dyn_itab
    form build_dyn_itab.
    * Create the dynamic internal table
      data: new_table type ref to data,
            new_line  type ref to data.
    * Create fields .
      do p_flds times.
        clear wa_fldcat.
        wa_fldcat-fieldname = sy-index.
        wa_fldcat-datatype  = 'CHAR'.
        wa_fldcat-intlen    = 5.
        append wa_fldcat to it_fldcat .
      enddo.
    * Create dynamic internal table and assign to FS
      call method cl_alv_table_create=>create_dynamic_table
                   exporting
                      it_fieldcatalog = it_fldcat
                   importing
                      ep_table        = new_table.
      assign new_table->* to <dyn_table>.
    * Create dynamic work area and assign to FS
      create data new_line like line of <dyn_table>.
      assign new_line->* to <dyn_wa>.
    endform.
    Have to split this code into two postings to keep formatting.
    Regards,
    Rich Heilman
    Edited by: Rich Heilman on Jun 23, 2009 11:59 AM

Maybe you are looking for

  • Send a mail to the vendor at the time of approval with the attachment of PO

    Hi all, I want to send a mail to the vendor at the time of approval by attaching the pdf of that particular PO. How can I achieve this? I know the User Exit where the code should be written. But I want the solution for attaching the PDF of PO at the

  • Sql query tuning required

    Dear Experts, I have a sql query which taking more than 2 hour of time ot execute. the explain plan is : PLAN_TABLE_OUTPUT Plan hash value: 2694368390 | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | | 0 | SELECT STATEMENT | | 1 | 379 | 4

  • Error Occurred Whilst Processing Request (Updated)

    I added a table to my page using the record insertion form wizard and when I went to view it in my browser I got the following error Error executing database query Syntax error in INSERT INTO statement.. This is code that is automatically  created by

  • Cisco 3825 as a EZ VPN Server

    I have a Cisco 3825 setup as a EZ VPN Server. I can connect and authenticate to it but I can't pass traffic (at least that's what it seems like). My internal network is 192.168.111.x and my VPN pool is 10.13.0.x. I am succesfully assigned an IP from

  • How can you add a 2nd i Tunes account on the same computer?

    How can you add a 2nd i Tunes account to the same computer?