2012 Feb 08 4:53 PM
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
2012 Feb 09 1:44 AM
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.
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
2012 Feb 09 1:44 AM
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.
2012 Feb 09 6:17 AM
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
2012 Feb 09 6:19 AM
Please explain the problem with the code you have written, Its really hard to understand what you are asking
2012 Feb 09 6:31 AM
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
2012 Feb 09 6:38 AM
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
2012 Feb 09 6:56 AM
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.
2012 Feb 09 7:42 AM
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
2012 Feb 09 7:47 AM
2012 Feb 09 9:02 AM
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