Skip to Content

Columns in a dynamic table

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Sep 09, 2009 at 02:01 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 09, 2009 at 02:07 PM

    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.
    
    

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 09, 2009 at 02:10 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.