Skip to Content
author's profile photo
Former Member

Dynamic field modification in Work Area.

Hi,

I want to insert 1 record in a transparent table. (But for time being, let us assume the table is MARA)

The columns and table name to which data is to be inserted are stored in another transparent table say (z1354_inv_rec_dt).

The data to be inserted is specified in a text file.

The value for MATNR, let us assume is hard coded as 1252.

The value for fields ERSDA, ERNAM, LAEDA and AENAM will be specified in input text file.

Also the table z1354_inv_rec_dt, will have rows with values ERSDA, ERNAM, LAEDA and AENAM.

How do I do this, the code specified in form comparedata is not correct one, but just a starting point for code.

<b>So how do I specify the column of work area at run time and assign value to it.</b>

Then I have to append value from work area to internal table.

And from internal table, I will insert to database.

REPORT ZDYN_INS_MARA.
TYPE-POOLS : abap.
DATA:
  lit_z1354_inv_rec_dt  TYPE TABLE OF z1354_inv_rec_dt,
* table z1354_inv_rec_dt contains 2 columns column_name and table_name
* values for column_name are ERSDA, ERNAM, LAEDA and AENAM
* value for table_name is MARA.

  lwa_invoice LIKE  z1354_inv_rec_dt,
  lit_mara TYPE TABLE OF mara,
  lwa_mara LIKE mara.

* To get column names
DATA :
  it_details TYPE abap_compdescr_tab,
  wa_details TYPE abap_compdescr,
  ref_descr TYPE REF TO cl_abap_structdescr.

FIELD-SYMBOLS : <fs_mara> TYPE mara.
FIELD-SYMBOLS : <fs_wa_mara> LIKE LINE OF lit_mara.
FIELD-SYMBOLS : <dyn_field>.

DATA: int_tabname(30).
int_tabname = 'MARA'.

*Populate internal table with columns to be added to Database
PERFORM populaterecdt.
ref_descr ?= cl_abap_typedescr=>describe_by_name( int_tabname ).
*Get the column names from the Database tables
it_details[] = ref_descr->components[].

PERFORM comparedata.

*&---------------------------------------------------------------------*
*&      Form  PopulateRecDt
*&---------------------------------------------------------------------*
FORM populaterecdt .
  SELECT column_name table_name FROM z1354_inv_rec_dt
  INTO CORRESPONDING FIELDS OF TABLE lit_z1354_inv_rec_dt
  WHERE table_name = 'MARA' .

ENDFORM.                    " PopulateRecDt

*&---------------------------------------------------------------------*
*&      Form  CompareData
*&---------------------------------------------------------------------*
FORM comparedata .
*loop through the DB table which holds the column names to be added
LOOP AT lit_z1354_inv_rec_dt INTO lwa_invoice.
*Loop throught table which holds the column names
  ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_wa_mara> TO <dyn_field>.
  <dyn_field> = '1252'   .

  LOOP AT it_details INTO wa_details.
    IF wa_details-name = lwa_invoice-column_name.
* Assign value to column of work area from text file.
      ASSIGN component lwa_invoice-column_name of structure                   <fs_wa_mara> to <dyn_field>.
*Get value for <dyn_field> from text file
        <dyn_field> = 'XYZ212'.

    ENDIF.
  ENDLOOP.
ENDLOOP.
*append work area t to lit_mara.
*Append work area to internal table.
*insert the internal table to DB
ENDFORM.                    " CompareData

Here in this loop, I want to assign to the work area for Mara based on the column values, How do I do it.

Regards,

Vikas

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • author's profile photo
    Former Member
    Posted on Sep 28, 2007 at 09:43 AM

    Hi,

    This problem has been resolved.

    DATA:
        lit_MARA TYPE TABLE OF MARA,
        lit2_MARA TYPE TABLE OF MARA,
        lwa_MARA LIKE MARA,
        lit_inv TYPE TABLE OF z1354_inv_rec_dt,
        wa_inv  LIKE z1354_inv_rec_dt,
        sctemp(100) TYPE c,
        ifs type i,
         p_gs_s_rec_line TYPE string.
    
      FIELD-SYMBOLS:
        <fs_MARA> TYPE table,
        <ld_column>   TYPE ANY,
        <fs_wa_MARA> LIKE LINE OF lit_MARA.
    
    *Populate Invoice table
      SELECT * FROM  z1354_inv_rec_dt INTO CORRESPONDING FIELDS OF TABLE
      lit_inv WHERE recordtype = 'S' AND table_name = 'MARA' AND
      column_seq > 0.
      
      p_gs_rec_line = '1234567890000001'.
    
      lwa_MARA-laufi = '123e'.
      FREE lit_MARA.
      APPEND  lwa_MARA TO lit_MARA.
      ASSIGN lit_MARA[] TO <fs_MARA>.
      UNASSIGN: <fs_wa_MARA>.
    * free lit_MARA[] .
    
    * LOOP AT <fs> ASSIGNING <fs1>.
     LOOP AT lit_MARA ASSIGNING <fs_wa_MARA>.
      ifs = sy-tabix.
      write: / ifs.
    
        LOOP AT lit_inv INTO wa_inv.
    
          WRITE
          p_gs_s_rec_line+wa_inv-start_pos_extrac(wa_inv-chars_to_extract)
          TO sctemp.
    
          UNASSIGN: <ld_column>.
          ASSIGN COMPONENT wa_inv-column_name OF STRUCTURE <fs_wa_MARA>
          TO <ld_column>.
          <ld_column> =  sctemp.
          IF ( <ld_column> IS ASSIGNED ).
            WRITE: / '.' .
          ENDIF.
          CLEAR wa_inv.
        ENDLOOP.
        MOVE  <fs_wa_MARA> TO lwa_MARA.
        APPEND lwa_MARA TO lit2_MARA.
       CLEAR <fs_wa_MARA>.
      ENDLOOP.
    loop at lit2_MARA into lwa_MARA.
      write: / lwa_MARA-filename.
      clear lwa_MARA.
    endloop.
    insert MARA from table lit2_MARA.
    if sy-subrc <> 0.
      write: / ' Insert  :-( '.
    endif.
    

    Add comment
    10|10000 characters needed characters exceeded