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: 

Find the number column of a internal table

Former Member
0 Kudos

Hi all,

At the runtime I need to find the number of column of a internal table . For example for finding the number of rows we will be using DESCRIBE TABLE ...... like that do we have any statemnt.

regards

paul

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi paul,

1. GET_COMPONENT_LIST

use this FM

2. not only the column number,

u will get all column details.

column name,

data type

length .

3. eg. (just copy paste in new program)

REPORT abc.

DATA : cmp LIKE TABLE OF rstrucinfo WITH HEADER LINE.

DATA : pa0001 LIKE TABLE OF pa0001 WITH HEADER LINE.

CALL FUNCTION 'GET_COMPONENT_LIST'

EXPORTING

program = sy-repid

fieldname = 'PA0001'

TABLES

components = cmp.

LOOP AT cmp.

WRITE 😕 cmp-compname , cmp-TYPE , cmp-olen.

ENDLOOP.

regards,

amit m.

9 REPLIES 9

Former Member
0 Kudos

Hi Paul

Number of columns in Internal Tables are fixed and doesnt and cant vary while execution so, that you know when you create internal table.

In case of extracts you have different number of columns for different type of records/lines.

Thanks

Former Member
0 Kudos

Hi paul,

1. GET_COMPONENT_LIST

use this FM

2. not only the column number,

u will get all column details.

column name,

data type

length .

3. eg. (just copy paste in new program)

REPORT abc.

DATA : cmp LIKE TABLE OF rstrucinfo WITH HEADER LINE.

DATA : pa0001 LIKE TABLE OF pa0001 WITH HEADER LINE.

CALL FUNCTION 'GET_COMPONENT_LIST'

EXPORTING

program = sy-repid

fieldname = 'PA0001'

TABLES

components = cmp.

LOOP AT cmp.

WRITE 😕 cmp-compname , cmp-TYPE , cmp-olen.

ENDLOOP.

regards,

amit m.

former_member181962
Active Contributor
0 Kudos

Hi Paul,

The number of columns will be static and i think there wil not be any syntax to get the same.

REgards,

Ravi

naimesh_patel
Active Contributor
0 Kudos

Hello,

DATA: IT_COMP LIKE RSTRUCINFO OCCURS 0 WITH HEADER LINE.

data: P_tab LIKE DD03L-TABNAME. "Internal table

CALL 'AB_STRUC_INFO'

ID 'PROGRAM' FIELD SY-CPROG

ID 'STRUCNAME' FIELD P_ITAB

ID 'STRUCINFO' FIELD IT_COMP-SYS.

Table IT_COMP will give you a list of all fields of Internal table.

describe table it_comp lines sy-index. will give you number of fields of internal table.

Regards,

Naimesh

former_member188685
Active Contributor
0 Kudos

Hi,

you can find the number of columns and their order using

the <b>'REUSE_ALV_FIELDCATALOG_MERGE'</b>

 call function 'REUSE_ALV_FIELDCATALOG_MERGE'
 EXPORTING
   I_PROGRAM_NAME               = sy-repid
   I_INTERNAL_TABNAME           = 'ITAB'
   I_INCLNAME                   = sy-repid
  changing
    ct_fieldcat                  = IT_FIELDCAT
 EXCEPTIONS
   INCONSISTENT_INTERFACE       = 1
   PROGRAM_ERROR                = 2
   OTHERS                       = 3
          .
if sy-subrc <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif

now describe your fieldcat . and find no of columns.

and their order also..

Regards

vijay

0 Kudos

I always use the classes below.

CL_ABAP_CLASSDESCR
CL_ABAP_COMPLEXDESCR
CL_ABAP_DATADESCR
CL_ABAP_ELEMDESCR
CL_ABAP_INTFDESCR
CL_ABAP_OBJECTDESCR
CL_ABAP_REFDESCR
CL_ABAP_STRUCTDESCR
CL_ABAP_TABLEDESCR
CL_ABAP_TYPEDESCR

Good luck!

Best regards, Sander Pol

Former Member
0 Kudos

The SYST (sy-) variable seems to be SCOL (look at content of SYST in debug after your describe statement).  However, one needs to consider that .include's are apparently counted as a column.

Former Member

Hi,

Try this simple way of counting columns of your internal table

DATA: w_count LIKE sy-tabix.

FIELD-SYMBOLS: <fs>.

DATA: BEGIN OF itab OCCURS 0,
         text1   TYPE c,
         text2   TYPE c,
         text3   TYPE c,
         num1  TYPE n,
         num2  TYPE n,
         num3  TYPE n,
       END OF itab.

CLEAR w_count.

DO.
   ADD 1 TO w_count.
   ASSIGN COMPONENT w_count OF STRUCTURE itab TO <fs>.
   IF sy-subrc <> 0.
     EXIT.
   ENDIF.
ENDDO.

SUBTRACT 1 FROM w_count.

WRITE: w_count.

Regards,

Jake

Former Member
0 Kudos

Hi,

assign the structure to a field symbol and use assign-components ... press F1 help on assign components...


if sy-index = 1.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_record> TO <fs_comp>.
IF sy-subrc <> 0.
count = count + 1.
ENDIF.
endif.

Otherwise use the below code,

DO.

  ASSIGN COMPONENT SY-INDEX OF STRUCTURE GWA_ITAB TO <FS>.

  IF SY-SUBRC = 0.

    CONTINUE.

  ELSE.

    G_COLNUMBER = SY-INDEX.

    EXIT.

  ENDIF.

ENDDO.

Now G_COLNUMBER - 1 , will be the no of column.

Regards,

Arun