Skip to Content
-1

What exactly the use of Assign Component?

Hi Friends,

I want to know what exactly the use of assign component......

below is the code using which I am updating the value in work area as well as internal table....

But my doubt is we can do directly by passing value to the work area itself.

like....

select * from but000 into table gt_tab UP TO 10 rows.

loop at gt_tab assigning <fs_tab>.
  <fs_tab>-bpkind = 'SOMENAME'.
endloop.

Why we are use assign component here....

both are doing the same work...either we can do it by passing directly or by using assign component.....

select * from but000 into table gt_tab UP TO 10 rows.

loop at gt_tab assigning <fs_tab>.
  assign component 'BPKIND' of structure <fs_tab> to <fs_field>.
  <fs_field> = 'SOMENAME'.
endloop.

please help me...

Thanks,

Divi.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    avatar image
    Former Member
    Jan 27, 2017 at 09:15 AM
    -1

    Hi Divi,

    If you don't know the field name in the internal table which you are updating the value, till u execute the program (dynamic like TYPE ANY), then u will use ASSIGN COMPONENT. In the below example, based on the parameter field name entered, we will assign data to that particular field in the internal table.

    DATA: BEGIN OF line,
    col1 TYPE i VALUE 11,
    col2 TYPE i VALUE 22,
    col3 TYPE i VALUE 33,
    END OF line.

    PARAMETERS: p_field TYPE C.

    FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE ANY, <f3> TYPE ANY.

    ASSIGN line TO <f1>.
    ASSIGN p_field TO <f2>.

    DO 3 TIMES.
    ASSIGN COMPONENT sy-index OF STRUCTURE <f1> TO <f3>.
    WRITE <f3>.
    ENDDO.

    ASSIGN COMPONENT <f2> OF STRUCTURE <f1> TO <f3>.
    WRITE / <f3>.

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 27, 2017 at 08:57 AM

    To understand by yourself,

    1. read the online/F1 documentation on statements LOOP ASSIGNING <FS> and MODIFY (itab)
    2. think 'dynamic'

    Regards,
    Raymond

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 27, 2017 at 09:11 AM

    If you can do it statically (direct), you don't need ASSIGN COMPONENT.

    If you have to program dynamically, that is you know the name or the position of a component only at runtime (not at compile time), you can use ASSIGN COMPONENT.

    Maybe have a look at the following:

    http://help.sap.com/abapdocu_751/en/index.htm?file=abendyn_access_data_obj_guidl.htm

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 27, 2017 at 09:10 AM

    Consider what what would happen in your first example if GT_TAB were an importing parameter to a method, with type ANY TABLE.

    <fs_tab> is a very poor choice of field symbol name. For a start, why use fs? The angle brackets alone tell you it's a field symbol. Further, use meaningful names. And according to the Official ABAP Programming Guidelines, using prefixes to denote types isn't best practice.

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 27, 2017 at 09:36 AM

    Hi,


    You can use it also with a field index, here you have some info;

    http://help.sap.com/SAPhelp_nw70/helpdata/en/fc/eb3923358411d1829f0000e829fbfe/content.htm

    As Raymond said think 'dynamic'.

    For example, image you have a table with the following strucure where you have a field with a table name and other field with the content;

     TYPES:
       BEGIN OF tys_data,
         tabname TYPE Tabname,
         content TYPE EDI_SDATA,
       END OF tys_data,
       tyt_data TYPE STANDARD TABLE OF tys_data.

    And you know there will be always a field called LIFNR that you need to print. But you dont know the name of the table in each record.

    You can do something like this;

    Creating a variable for each table type; ( although maybe you dont know all the table types in "design" time)

      DATA:
        lt_data    TYPE tyt_data,
        ls_lfa1    TYPE lfa1,
        ls_lfb1    TYPE lfb1,
        ls_ekko    TYPE ekko.
    *      ...      
    
    
       FIELD-SYMBOLS:
        <ls_data>    TYPE tys_data.
    
    
      LOOP AT lt_data
        ASSIGNING <ls_data>.    
    
        CASE <ls_data>-tabname.
          WHEN 'LFA1'.
            MOVE <ls_data>-content TO ls_lfa1.
            WRITE:\ ls_lfa1-lifnr.
    
          WHEN 'LFB1'.
            MOVE <ls_data>-content TO ls_lfb1.
            WRITE:\ ls_lfb1-lifnr.
    
          WHEN 'EKKO'.
            MOVE <ls_data>-content TO ls_ekko.
            WRITE:\ ls_ekko-lifnr.        
    
    *      ...               
    
          WHEN OTHERS.
    
        ENDCASE. 
    
      ENDLOOP.

    Or you can do it dynamically like this:

      DATA:
        lo_content TYPE REF TO data,
        lt_data    TYPE tyt_data.
    
       FIELD-SYMBOLS:
        <ls_data>    TYPE tys_data,
        <ls_content> TYPE any,
        <lv_field>   TYPE any.
    
      LOOP AT lt_data
        ASSIGNING <ls_data>.
    
        CREATE DATA lo_content TYPE (<ls_data>-tabname).
        CHECK sy-subrc EQ 0.
    
        ASSIGN lo_content->* TO <ls_content>.
        CHECK sy-subrc EQ 0.
    
        ASSIGN COMPONENT 'LIFNR' OF STRUCTURE <ls_content> TO <lv_field>.
        CHECK sy-subrc EQ 0.
    
        WRITE: <lv_field>.
    
      ENDLOOP.

    This is only a silly example, but you can figure what you can do with this.

    Regards,

    Ricardo.

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 27, 2017 at 03:49 PM

    If you are using ASSIGN COMPONENT in a loop, that can have a performance hit. Instead of using

    LOOP AT <generic_table> ASSIGNING FIELD-SYMBOL(<generic_record>).
      ASSIGN COMPONENT 'ACTUAL_FIELD_NAME' OF STRUCTURE <generic_record> TO FIELD-SYMBOL(<field_value>).
      WRITE <field_value>.
    ...

    use

    DATA: BEGIN OF record,
            actual_field_name TYPE string, " actual fieldname, with actual type
            ...
          END OF record.
    
    LOOP AT <generic_table> ASSIGNING FIELD-SYMBOL(<generic_record>).
      MOVE-CORRESPONDING <generic_record> TO record.
      WRITE record-actual_field_name.
    ...

    Not only is it faster, I think it's also easier to understand - and to set watch points. If you need to update field values, you use

    ...
      record-actual_field_name = 'New value'.
      MOVE-CORRESPONDING record TO <generic_record>.
    ENDLOOP.
    Add comment
    10|10000 characters needed characters exceeded