Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Add line to type ref to data field - type table

Former Member
0 Likes
7,481

HI All,

I have field that is defined as type ref to data ,and the type of it is table with data inside of it lr_data.

I have the also the table type of lr_data and I want to add additional line to the type ref to data field.

How can i do that ?

For example

 TYPES: BEGIN OF bank_list,
         country_key type string,
         bank_key    type string ,
         bank_name   type string,
         bank_city  type string    ,
       END OF bank_list.

data lt_bank type table of bank_list.

data lr_data data  ref to data.

Now lr_datae is  type lt_bank " in _Runtine
and contain 4 entries .

I get the lr_data with 4 entries and I want to add new entry to the table.

How can I do that?

Thanks,

Joy

Edited by: Joy Stpr on Feb 8, 2012 10:05 PM

1 ACCEPTED SOLUTION
Read only

brunobex
Active Participant
0 Likes
4,233

Joy Stpr,

I do not know if I understand the question but see if code below can help:


TYPES: BEGIN OF bank_list,
        country_key TYPE string,
        bank_key    TYPE string,
        bank_name   TYPE string,
        bank_city   TYPE string,
      END OF bank_list,

      begin of ref_data,
        data type ref to data,
      end   of ref_data,
      ty_t_ref_data type STANDARD TABLE OF ref_data.

DATA:
     lt_bank TYPE TABLE OF bank_list,
     lw_bank LIKE LINE OF lt_bank.

DATA:
     lr_data TYPE ty_t_ref_data,
     lw_data LIKE LINE OF lr_data.

field-symbols:
     <f_bank_line> like line of lt_bank.

lw_bank-country_key = 'BR'.
INSERT lw_bank INTO TABLE lt_bank
          REFERENCE INTO lw_data-data.

INSERT lw_data INTO TABLE lr_data.

loop at lr_data into lw_data.
  UNASSIGN <f_bank_line>.
  assign lw_data-data->* to <f_bank_line>.
  if <f_bank_line> is assigned.
    write: <f_bank_line>-country_key.
  endif.
endloop.

Regards

Bruno Xavier.

9 REPLIES 9
Read only

brunobex
Active Participant
0 Likes
4,234

Joy Stpr,

I do not know if I understand the question but see if code below can help:


TYPES: BEGIN OF bank_list,
        country_key TYPE string,
        bank_key    TYPE string,
        bank_name   TYPE string,
        bank_city   TYPE string,
      END OF bank_list,

      begin of ref_data,
        data type ref to data,
      end   of ref_data,
      ty_t_ref_data type STANDARD TABLE OF ref_data.

DATA:
     lt_bank TYPE TABLE OF bank_list,
     lw_bank LIKE LINE OF lt_bank.

DATA:
     lr_data TYPE ty_t_ref_data,
     lw_data LIKE LINE OF lr_data.

field-symbols:
     <f_bank_line> like line of lt_bank.

lw_bank-country_key = 'BR'.
INSERT lw_bank INTO TABLE lt_bank
          REFERENCE INTO lw_data-data.

INSERT lw_data INTO TABLE lr_data.

loop at lr_data into lw_data.
  UNASSIGN <f_bank_line>.
  assign lw_data-data->* to <f_bank_line>.
  if <f_bank_line> is assigned.
    write: <f_bank_line>-country_key.
  endif.
endloop.

Regards

Bruno Xavier.

Read only

Former Member
0 Likes
4,233

HI Bruno,

The issue here is that I need to add new line (with new bank as you write down )to the table lr_data but its not working .

Thanks,

Joy

Read only

0 Likes
4,233

Please explain the problem with the code you have written, Its really hard to understand what you are asking

Read only

Former Member
0 Likes
4,233

HI Bruno,

The issue here is that I need to add new line (with new bank as you write down )to the table lr_data but its not working .

lr_data is not defined as table but type ref to data can have table there during RT this is the problem here .

In your example you define it as table.and when I want to do append i got an error said that lr_data

is not defined as table so i cant use insert/append.

Thanks,

Joy

Edited by: Joy Stpr on Feb 9, 2012 7:35 AM

Read only

Former Member
0 Likes
4,233

HI Keshav,

The issue here is that I need to add new line (with new bank as you write down )to the table lr_data but its not working .

lr_data is not defined as table but type ref to data can have table there during RT this is the problem here .

when I want to do append i got an error said that lr_data

is not defined as table so i cant use insert/append.

Thanks,

Joy

Read only

0 Likes
4,233

I hope the below code clears your doubt.


field-symbols:<fs_tab> type any.
field-symbols:<fs> type any.
field-symbols:<field> type any.
data:wf_refl type ref to data.

assign lr_data->* to <fs_tab>. "<--
create data wf_refl like line of <fs_tab>.
assign wf_refl->* to <fs>.

Now assume that <fs_tab> has two fields field1 and field2.

to append a row to <fs_tab>.


do 2 times.
assign component sy-index of structure <fs> to <field>.
if sy-subrc = 0.
<field> = 'ABC'.
else.
append <fs> to <fs_tab>.
endif.
enddo.

Read only

Former Member
0 Likes
4,233

HI

I use your code but how I insert all the fields to the same line in the table ?

ASSIGN COMPONENT 'BANK_KEY'  OF STRUCTURE <fs> TO <field>.
  <field> = '123'.

   ASSIGN COMPONENT 'BANK_NAME'  OF STRUCTURE <fs> TO <field1>.
  <field1> = 'My Bank'.

  ASSIGN COMPONENT 'BANK_CITY'  OF STRUCTURE <fs> TO <field2>.
  <field2> = 'My city'.

Thanks,

Joy

Read only

0 Likes
4,233

Paste your code

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
4,233

Hello Joy,

You can check this code snippet to get an idea on how to proceed with your requirement:


CLASS lcl_add_data DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS add_data CHANGING ch_data TYPE REF TO data
                           EXCEPTIONS err_invalid_type.
ENDCLASS.                    "lcl_add_data DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_add_data IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_add_data IMPLEMENTATION.

  METHOD add_data.

    DATA: lo_tabdescr   TYPE REF TO cl_abap_tabledescr,
          lo_strucdescr TYPE REF TO cl_abap_structdescr,
          ls_component  TYPE abap_compdescr.

    FIELD-SYMBOLS: <lt_data>      TYPE STANDARD TABLE,
                   <ls_data>      TYPE any,
                   <lv_fieldval>  TYPE any.

    IF ch_data IS BOUND.
*     Get the runtime type object
      lo_tabdescr  ?= cl_abap_typedescr=>describe_by_data_ref( ch_data ).

*     Check  if supplied param is table
      IF lo_tabdescr->type_kind NE cl_abap_typedescr=>typekind_table.
        RAISE err_invalid_type.
      ENDIF.
    ELSE.
      RETURN.
    ENDIF.

    ASSIGN ch_data->* TO <lt_data>. "Dereferencing the data reference

*   Get the line type of table
    lo_strucdescr ?= lo_tabdescr->get_table_line_type( ).

    DO 2 TIMES.
*     Add initial line to the table
      APPEND INITIAL LINE TO <lt_data> ASSIGNING <ls_data>.
      CLEAR <ls_data>.

*     Loop on the fields of the structure & populate them
      LOOP AT lo_strucdescr->components INTO ls_component.
        ASSIGN COMPONENT ls_component-name OF STRUCTURE <ls_data> TO <lv_fieldval>.
        CASE ls_component-name.
          WHen 'COUNTRY_KEY'.
            <lv_fieldval> = `My Country`.
          WHEN 'BANK_KEY'.
            <lv_fieldval> = `123`.
          WHEN 'BANK_NAME'.
            <lv_fieldval> = `My Bank`.
          WHEN 'BANK_CITY'.
            <lv_fieldval> = `My City`.
        ENDCASE.
      ENDLOOP.
    ENDDO.

  ENDMETHOD.                    "add_data

ENDCLASS.                    "lcl_add_data IMPLEMENTATION

TYPES:
BEGIN OF bank_list,
  country_key TYPE string,
  bank_key    TYPE string,
  bank_name   TYPE string,
  bank_city   TYPE string,
END OF bank_list.

DATA: lt_bank TYPE STANDARD TABLE OF bank_list,
      lr_data TYPE REF TO data.

START-OF-SELECTION.

  GET REFERENCE OF lt_bank INTO lr_data. "Get reference

* Add the data to the data reference
  lcl_add_data=>add_data( CHANGING    ch_data = lr_data
                          EXCEPTIONS  err_invalid_type = 1 ).

BR,

Suhas