08-11-2017 1:53 PM
Hi, alll,
I have a task.
To display number of columns of internal table. Can you help me how to do that?
Some smart function?
I know how to display number of lines.
describe table name_table lines num_lines.
write num_lines.
08-11-2017 3:31 PM
You can use RTTI's structdescriptor for this.
https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenrtti.htm
08-11-2017 3:31 PM
You can use RTTI's structdescriptor for this.
https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenrtti.htm
08-11-2017 10:53 PM
Depending on your needs maybe one of this option is also useful
**********************************************************************
* Static DDIC Table with details
DATA: lt_dfies_but000 TYPE TABLE OF dfies.
CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
tabname = 'BUT000' "<== Table
TABLES
dfies_tab = lt_dfies_but000
EXCEPTIONS
OTHERS = 1.
DATA(lv_but000_field_no) = lines( lt_dfies_but000 ).
**********************************************************************
* Static DDIC Structure with details
DATA: lt_dfies_eew_but000 TYPE TABLE OF dfies.
CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
tabname = 'INCL_EEW_BUT000' "<== Structure
TABLES
dfies_tab = lt_dfies_eew_but000
EXCEPTIONS
OTHERS = 1.
DATA(lv_eew_but000_field_no) = lines( lt_dfies_eew_but000 ).
**********************************************************************
* Some Dynamic Structure - just count
DATA: ls_adrc TYPE adrc.
DATA: lv_index TYPE i.
FIELD-SYMBOLS: <lv_any_field> TYPE any.
DO.
lv_index = lv_index + 1.
ASSIGN COMPONENT lv_index OF STRUCTURE ls_adrc TO <lv_any_field>.
IF sy-subrc <> 0.
EXIT. "DO
ENDIF.
ENDDO.
DATA(lv_adrc_field_no) = lv_index - 1.
08-12-2017 6:15 PM
code below:
the number of columns is still 1! Why do that? 😞
*&---------------------------------------------------------------------*
*& Report Z_DB_TABLE_COL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT Z_DB_TABLE_COL.
PARAMETERS: p_table LIKE dd03l-tabname. " vložení názvu tabulky s možností filtrace
FIELD-SYMBOLS:
<fs_table> TYPE table, " zavedení symbolů pro název tabulky
<wa_table> TYPE any,
<fs>.
DATA: xtable TYPE REF TO data, " tohle dává zprávu překladači, že data objektového typu budou definované za běhu
lin_table type i,
col_table TYPE i.
CREATE DATA xtable TYPE TABLE OF (p_table)." vytváření dat XTABLE za běhu
ASSIGN xtable->* TO <fs_table>. " před použitím symbolu pole loop, musí být přiřazeno
SELECT * FROM (p_table) INTO TABLE <fs_table> ORDER BY PRIMARY KEY. " výběr DB tabulky a přeházení dat do interní tabulky
LOOP AT <fs_table> ASSIGNING <wa_table>. " smyčka pro čtení tabulky
* write: / <wa_table>.
ENDLOOP.
DESCRIBE TABLE <fs_table> LINES lin_table.
DATA: lv_index TYPE i.
FIELD-SYMBOLS: <lv_any_field> TYPE any.
DO.
lv_index = lv_index + 1.
ASSIGN COMPONENT lv_index OF STRUCTURE <fs_table> TO <lv_any_field>.
IF sy-subrc <> 0.
EXIT. "DO
ENDIF.
ENDDO.
DATA(lv_adrc_field_no) = lv_index - 1.
write: / 'lines: ',lin_table, 'columns:', <lv_any_field>.
08-13-2017 9:13 AM
you need to use the structure <wa_table>
The number of columns will be in lv_adrc_field_no
...
DO.
lv_index = lv_index + 1.
ASSIGN COMPONENT lv_index OF STRUCTURE <wa_table> TO <lv_any_field>. "USE <wa_table>
IF sy-subrc <> 0.
EXIT. "DO
ENDIF.
ENDDO.
DATA(lv_adrc_field_no) = lv_index - 1.
write: / 'lines: ',lin_table, 'columns:', lv_adrc_field_no. "USE lv_adrc_field_no
08-13-2017 9:21 AM
*SY-TABIX CONTAINS INTERNAL TABLE INDEX NO
DATA: COUNT LIKE SY-TABIX.
FIELD-SYMBOLS: <FS>.
Data:BEGIN OF ITAB OCCURS 0 ,
NAME(10) TYPE C,
AGE TYPE I,
SAL TYPE I,
END OF ITAB.
CLEAR COUNT.
DO.
ADD 1 TO COUNT.
ASSIGN COMPONENT COUNT OF STRUCTURE ITAB TO <FS> .
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
ENDDO.
SUBTRACT 1 FROM COUNT.
WRITE: 'NO OF COLUMNS:',COUNT.