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: 

Columns in a dynamic table

former_member204025
Participant
0 Kudos

Hi guys!

I'm working with field symbols and dynamic tables and i have this code:

REPORT zpractica03.

PARAMETERS: p_tabla TYPE dd02l-tabname.

DATA: tabla_name TYPE dd02l-tabname,

tabla_generica TYPE REF TO data,

linea_generica TYPE REF TO data.

FIELD-SYMBOLS: <fs_tabla> TYPE ANY TABLE,

<fs_linea> TYPE ANY,

<fs_campo> TYPE ANY.

tabla_name = p_tabla.

CREATE DATA tabla_generica TYPE STANDARD TABLE OF (tabla_name).

ASSIGN tabla_generica->* TO <fs_tabla>.

CREATE DATA linea_generica TYPE (tabla_name).

ASSIGN linea_generica->* TO <fs_linea>.

SELECT *

INTO CORRESPONDING FIELDS OF TABLE <fs_tabla>

FROM (tabla_name).

DATA: lv_indice_campo TYPE sy-tabix,

lv_valor_campo.

LOOP AT <fs_tabla> INTO <fs_linea>.

CLEAR lv_indice_campo.

lv_indice_campo = sy-tabix.

ASSIGN COMPONENT lv_indice_campo OF STRUCTURE <fs_linea> TO <fs_campo>.

IF sy-subrc EQ 0.

MOVE <fs_campo> TO lv_valor_campo.

ENDIF.

ENDLOOP.

In the last step, I know that I need something like this in order to get one line and save the info in a workarea and then create an ALV :

Do n Times

ASSIGN COMPONENT lv_indice_campo OF STRUCTURE <fs_linea> TO <fs_campo>.

IF sy-subrc EQ 0.

MOVE <fs_campo> TO lv_valor_campo.

ENDIF.

Enddo.

What I don't know how to do is how can I get the number 'N' ? Can somebody explain me how can I get the number of the columns in a table that can be different any time that i run the program?

Thank you very much.

Gaby

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor
0 Kudos

Hello,

You can use the FM: DDIF_FIELDINFO_GET & pass the table name tabla_name.

It will return the table DFIES_TAB which will contain the column details. So before this:

Do n Times
lv_indice_campo = lv_indice_campo + 1.
ASSIGN COMPONENT lv_indice_campo OF STRUCTURE <fs_linea> TO <fs_campo>.
IF sy-subrc EQ 0.
MOVE <fs_campo> TO lv_valor_campo.
ENDIF.
Enddo.

Add this code:


DATA: LT_DFIES        TYPE STANDARD TABLE OF DFIES.

    CALL FUNCTION 'DDIF_FIELDINFO_GET'
      EXPORTING
        TABNAME        = tabla_name
      TABLES
        DFIES_TAB      = LT_DFIES
      EXCEPTIONS
        NOT_FOUND      = 1
        INTERNAL_ERROR = 2
        OTHERS         = 3.
    IF SY-SUBRC = 0.
      N = LINES ( LT_DFIES ).
    ENDIF.

Else you can easily avoid this stuff by EXITing the DO...ENDDO loop.

Do.
lv_indice_campo = lv_indice_campo + 1.
ASSIGN COMPONENT lv_indice_campo OF STRUCTURE <fs_linea> TO <fs_campo>.
IF sy-subrc EQ 0.
  MOVE <fs_campo> TO lv_valor_campo.
ELSE.
  EXIT.
ENDIF.
Enddo.

Hope i am clear.

BR,

Suhas

6 REPLIES 6

SuhaSaha
Advisor
Advisor
0 Kudos

Hello,

You can use the FM: DDIF_FIELDINFO_GET & pass the table name tabla_name.

It will return the table DFIES_TAB which will contain the column details. So before this:

Do n Times
lv_indice_campo = lv_indice_campo + 1.
ASSIGN COMPONENT lv_indice_campo OF STRUCTURE <fs_linea> TO <fs_campo>.
IF sy-subrc EQ 0.
MOVE <fs_campo> TO lv_valor_campo.
ENDIF.
Enddo.

Add this code:


DATA: LT_DFIES        TYPE STANDARD TABLE OF DFIES.

    CALL FUNCTION 'DDIF_FIELDINFO_GET'
      EXPORTING
        TABNAME        = tabla_name
      TABLES
        DFIES_TAB      = LT_DFIES
      EXCEPTIONS
        NOT_FOUND      = 1
        INTERNAL_ERROR = 2
        OTHERS         = 3.
    IF SY-SUBRC = 0.
      N = LINES ( LT_DFIES ).
    ENDIF.

Else you can easily avoid this stuff by EXITing the DO...ENDDO loop.

Do.
lv_indice_campo = lv_indice_campo + 1.
ASSIGN COMPONENT lv_indice_campo OF STRUCTURE <fs_linea> TO <fs_campo>.
IF sy-subrc EQ 0.
  MOVE <fs_campo> TO lv_valor_campo.
ELSE.
  EXIT.
ENDIF.
Enddo.

Hope i am clear.

BR,

Suhas

former_member195698
Active Contributor
0 Kudos

try like this..



Do. 
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_linea> TO <fs_campo>.
IF sy-subrc NE 0.
   EXIT.
ENDIF.

MOVE <fs_campo> TO lv_valor_campo.
Enddo.

Former Member
0 Kudos

Hi

Just as a guys said u can use some fm in order to get the information about the table, but u should consider u don't need to know how many fields are in a table, u can use a condition to exit from the DO cycle:

 Do.
   ASSIGN COMPONENT lv_indice_campo OF STRUCTURE <fs_linea> TO <fs_campo>.
   IF sy-subrc EQ 0.
    MOVE <fs_campo> TO lv_valor_campo.
   else.
     exit.
  ENDIF.
Enddo.

Max

0 Kudos

Hi again guys!

Thank you very much for your help!!! One more question, should I keep the loop if I use the Do EndDo sentence?

Gaby

0 Kudos

You have to keep the Loop...

The loop is used for Looping at the Records in the Internal table... and the Do... Enddo.. is being used to move across the Columns within a single Record.

0 Kudos

Oh I see... I had a runtime error... I'll check what is wrong . Thank you very much!!!!