Application Development 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: 

ABAP: Check existence of column in internal table

rphss
Participant
0 Kudos

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>.

1 ACCEPTED SOLUTION

mateuszadamus
Active Contributor

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

13 REPLIES 13

mateuszadamus
Active Contributor

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

The second option sounds good and its working. Thanks

0 Kudos

I'm glad it worked.

Kind regards,
Mateusz

GK817
Active Contributor

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

bpawanchand
Active Contributor
0 Kudos

The statement expects a work areas/ structure memory are reference rather than an internal table

michael_piesche
Active Contributor

<<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.

0 Kudos

Hi,

I got an error

ASSIGN C_SOURCE_PACKAGE[1]..

Seems that the offset syntax [1] for internal table is not working.

0 Kudos

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.

rphss, please post your coding that throws the error and the exact error message. Is it a syntax error or a runtime error. Also, what is your ABAP version (e.g. 7.40, 7.53, etc.). You wrote "ASSIGN C_SOURCE_PACKAGE[1]..", but it should be ASSIGN C_SOURCE_PACKAGE[ 1 ] TO <SOURCE_FIELDS>.

0 Kudos

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.

0 Kudos
rphss Before 7.40, use the statement READ TABLE itab ... WITH KEY ... instead of table expression ("itab[ ... ]")

0 Kudos

ok Thanks to all for your input

Sandra_Rossi
Active Contributor
0 Kudos

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)"