2020 Sep 10 8:35 AM
Hi all,
I am trying to check if the c_source_package table (filled with 10 records) does have a column called
PERSON.
I have tried the below, but the second assign statement raises rc 4 .
Any idea?
.FIELD-SYMBOLS: <SOURCE_FIELDS> TYPE ANY,
<L_FS_YSTAT_NUM> TYPE ANY.
ASSIGN C_SOURCE_PACKAGE TO <SOURCE_FIELDS>.
ASSIGN COMPONENT 'PERSON' OF STRUCTURE <SOURCE_FIELDS> TO <L_FS_YSTAT_NUM>.
2020 Sep 10 8:47 AM
Hello rphss
I see two ways. RTTI:
DATA:
lo_table TYPE REF TO CL_ABAP_TABLEDESCR,
lo_row TYPE REF TO CL_ABAP_STRUCTDESCR.
lo_table ?= CL_ABAP_TABLEDESCR=>DESCRIBE_BY_DATA( c_source_package ).
lo_row ?= lo_table->GET_TABLE_LINE_TYPE( ).
DATA(lt_columns) = lo_row->GET_COMPONENTS( ).
READ TABLE lt_columns TRANSPORTING NO FIELDS WITH KEY name = 'PERSON'.
CHECK sy-subrc = 0.
And the other is trying to assign the column:
DATA:
ld_row TYPE REF TO data.
FIELD-SYMBOLS:
<lv_field> TYPE any,
<lv_row> TYPE any.
CREATE DATA ld_row LIKE LINE OF c_source_package.
ASSIGN ld_row->* TO <lv_row>.
ASSIGN COMPONENT 'PERSON' OF <lv_row> TO <lv_field>.
CHECK sy-subrc = 0.
Kind regards,
Mateusz
2020 Sep 10 8:47 AM
Hello rphss
I see two ways. RTTI:
DATA:
lo_table TYPE REF TO CL_ABAP_TABLEDESCR,
lo_row TYPE REF TO CL_ABAP_STRUCTDESCR.
lo_table ?= CL_ABAP_TABLEDESCR=>DESCRIBE_BY_DATA( c_source_package ).
lo_row ?= lo_table->GET_TABLE_LINE_TYPE( ).
DATA(lt_columns) = lo_row->GET_COMPONENTS( ).
READ TABLE lt_columns TRANSPORTING NO FIELDS WITH KEY name = 'PERSON'.
CHECK sy-subrc = 0.
And the other is trying to assign the column:
DATA:
ld_row TYPE REF TO data.
FIELD-SYMBOLS:
<lv_field> TYPE any,
<lv_row> TYPE any.
CREATE DATA ld_row LIKE LINE OF c_source_package.
ASSIGN ld_row->* TO <lv_row>.
ASSIGN COMPONENT 'PERSON' OF <lv_row> TO <lv_field>.
CHECK sy-subrc = 0.
Kind regards,
Mateusz
2020 Sep 10 1:05 PM
2020 Sep 10 1:08 PM
2020 Sep 10 8:48 AM
Hi,
If you want to check if particular field name exists in internal table structure, you may use CL_ABAP_TABLEDESCR. Check the answer here:
https://answers.sap.com/questions/65404/assign-component--of-structure-with-dynamic-work-a.html
To check field value (if column exists), l don't see any issue with your code at first glance.
Gaurav
2020 Sep 10 8:51 AM
The statement expects a work areas/ structure memory are reference rather than an internal table
2020 Sep 10 9:24 AM
<<I am trying to check if the c_source_package (internal) table does have a column called PERSON.>>
This is how I would do it.
DATA tabledescr TYPE REF TO cl_abap_tabledescr.
DATA strucdescr TYPE REF TO cl_abap_structdescr.
tabledescr ?= cl_abap_typedescr=>describe_by_data( c_source_package ).
strucdescr ?= tabledescr->get_table_line_type( ).
IF line_exists( strucdescr->components[ name = 'PERSON' ] ).
" ...
ENDIF.
With your attempted coding you have to do it slightly different.
" THIS IS WHAT YOU DO
" with your coding, you assign the entire internal table to a field-symbol
" assuming that c_source_package is an internal table, based on your description
ASSIGN C_SOURCE_PACKAGE TO <SOURCE_FIELDS>.
" now you want to assign a component of a structure <source_fields>
" which doesnt exist, because <source_fields> is a table
ASSIGN COMPONENT 'PERSON' OF STRUCTURE <SOURCE_FIELDS> TO <L_FS_YSTAT_NUM>.
" THIS IS WHAT YOU NEED TO DO INSTEAD
" with this coding it works, because you assign the first record of the table
" which is a structure, but it only works if table has records
ASSIGN C_SOURCE_PACKAGE[ 1 ] TO <SOURCE_FIELDS>.
" now you can assign a component of the structure <source_fields>
ASSIGN COMPONENT 'PERSON' OF STRUCTURE <SOURCE_FIELDS> TO <L_FS_YSTAT_NUM>.
IF <L_FS_YSTAT_NUM> IS ASSIGNED.
" ...
ENDIF.
2020 Sep 10 10:48 AM
Hi,
I got an error
ASSIGN C_SOURCE_PACKAGE[1]..
Seems that the offset syntax [1] for internal table is not working.
2020 Sep 10 11:18 AM
Hi Ralf,
It is not offset it is the index of the row. it is very similar to
READ TABLE itab into wa INDEX1 ~ wa = itab[ 1 ].
if this is not working then you need to check soemthing like
IF LINES( C_SOURCE_PACKAGE ) GT 0
ASSIGN C_SOURCE_PACKAGE[1]..
ENDIF.
2020 Sep 10 11:42 AM
2020 Sep 10 1:03 PM
Its A BW system and ABAP is 7.31
its a syntax error . I used your whole line in my coding, but the editor marks first [ already red while typing.
2020 Sep 10 1:18 PM
2020 Sep 10 1:28 PM
2020 Sep 10 12:37 PM
Your error is to consider that
ASSIGN C_SOURCE_PACKAGE TO <SOURCE_FIELDS>
allows to get the structure out of internal table C_SOURCE_PACKAGE, but you're wrong, it just do nothing else than making <SOURCE_FIELDS> point to the internal table itself, not the structure of its lines...
Simplest solution -> see the comments in Michael answer:
IF lines( C_SOURCE_PACKAGE ) >= 1.
ASSIGN C_SOURCE_PACKAGE[ 1 ] TO <SOURCE_FIELDS>.
...
"(here the rest of your code)"
Code before ABAP 7.40:
IF lines( C_SOURCE_PACKAGE ) >= 1.
READ TABLE C_SOURCE_PACKAGE INDEX 1 ASSIGNING <SOURCE_FIELDS>.
...
"(here the rest of your code)"