Hii Dileep,
For your alternative dynamic table creation with your desire no. of records, try with this sample code,
here the the dynamic internal table is created based upon the input table name.
In this sample code, displaying the records of a database table entered with the no. of records want to display in ALV grid. You can enter any table name and based on that the records will be fetched and displayed in ALV grid.
check out this code according to your relevant and necessity.
REPORT zr_05test_table.
PARAMETERS: p_tname TYPE tabname OBLIGATORY, "database table name
p_nrows TYPE i. "No.of rows
DATA: v_count TYPE c LENGTH 20,
t_fcat TYPE slis_t_fieldcat_alv,
x_layout TYPE slis_layout_alv,
w_dref TYPE REF TO data.
FIELD-SYMBOLS <t_tab> TYPE STANDARD TABLE.
AT SELECTION-SCREEN.
IF NOT p_tname IS INITIAL.
SELECT SINGLE tabname
FROM dd02l
INTO v_count
WHERE tabname EQ p_tname
AND as4vers EQ ' '
AND as4local EQ 'A'
AND tabclass NE 'INTTAB'
AND tabclass NE 'APPEND'.
IF sy-subrc <> 0.
MESSAGE 'No table name exist with entered value' TYPE 'E'.
ENDIF.
ENDIF.
IF p_nrows IS INITIAL.
SET CURSOR FIELD 'p_nrows'.
MESSAGE 'Enter The No. of rows' TYPE 'E'.
ENDIF.
START-OF-SELECTION.
CREATE DATA w_dref TYPE STANDARD TABLE OF (p_tname).
ASSIGN w_dref->* TO <t_tab>.
IF <t_tab> IS INITIAL.
SELECT *
FROM (p_tname)
INTO TABLE <t_tab>
UP TO p_nrows ROWS.
IF sy-subrc EQ 0.
PERFORM catalog.
PERFORM display.
ENDIF.
ENDIF.
*&---------------------------------------------------------------------*
*& Form CATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM catalog .
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE' "To create fieldcatalog
EXPORTING
i_program_name = sy-repid
i_structure_name = p_tname "put your structure name here
CHANGING
ct_fieldcat = t_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM. " CATALOG
*&---------------------------------------------------------------------*
*& Form DISPLAY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM display .
x_layout-zebra = 'X'.
x_layout-colwidth_optimize = abap_true.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
is_layout = x_layout
it_fieldcat = t_fcat
TABLES
t_outtab = <t_tab>
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM. " DISPLAY
Regards
Syed