Application Development and Automation 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: 
Read only

Code required

Former Member
0 Likes
693

Hello All,

I have some requirement as below:

on the selection screen the user will give some table name. After this i need to select the data from the table which the user has selected and i need to display the data.

can any post the code for this.

Regards,

Lisa.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
652

Hello,

Check thsi.


type-pools: slis.
 
field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.
 
data: alv_fldcat type slis_t_fieldcat_alv,
      it_fldcat type lvc_t_fcat.
 
type-pools : abap.
 
data : it_details type abap_compdescr_tab,
       wa_details type abap_compdescr.
 
data : ref_descr type ref to cl_abap_structdescr.
 
data: new_table type ref to data,
      new_line  type ref to data,
      wa_it_fldcat type lvc_s_fcat.
 
selection-screen begin of block b1 with frame title text .
parameters: p_table(30) type c.
selection-screen end of block b1.
 
 
* Get the structure of the table.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].
 
loop at it_details into wa_details.
  clear wa_it_fldcat.
  wa_it_fldcat-fieldname = wa_details-name .
  wa_it_fldcat-datatype = wa_details-type_kind.
  wa_it_fldcat-inttype = wa_details-type_kind. 
  wa_it_fldcat-intlen = wa_details-length.
  wa_it_fldcat-decimals = wa_details-decimals.
  append wa_it_fldcat to it_fldcat .
endloop.
 
* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
             exporting
                it_fieldcatalog = it_fldcat
             importing
                ep_table        = new_table.
 
assign new_table->* to <dyn_table>.
 
* Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.
 
* Select Data from table.
select * into table <dyn_table>
           from (p_table).
 
* Write out data from table.
loop at <dyn_table> into <dyn_wa>.
  do.
    assign component  sy-index  of structure <dyn_wa> to <dyn_field>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ <dyn_field>.
    else.
      write: <dyn_field>.
    endif.
  enddo.
endloop.

REgards,

Vasanth

5 REPLIES 5
Read only

Peter_Inotai
Active Contributor
0 Likes
652

Hi Lisa,

Check the 'Best of ABAP - The Ultimate ABAP 6.40 Feature Show' presentation/recorded session in the ABAP eLearning catalog for dymanic programing.

Or check the generated program in Se16/Se16N.

Best regards,

Peter

Read only

former_member150733
Contributor
0 Likes
652

REPORT zzz .

TYPE-POOLS slis.

PARAMETERS: pa_dbtab(20) DEFAULT 'T001' OBLIGATORY.

*DATA it_fields TYPE TABLE OF dd03l WITH HEADER LINE.

DATA: new_table TYPE REF TO data.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,

<dyn_wa>.

START-OF-SELECTION.

BREAK-POINT.

  • SELECT * FROM dd03l INTO TABLE it_fields WHERE tabname EQ pa_dbtab.

  • PERFORM fb_table_create TABLES it_fields

  • CHANGING new_table.

PERFORM fb_table_create CHANGING new_table.

ASSIGN new_table->* TO <dyn_table>.

SELECT * FROM (pa_dbtab) INTO TABLE <dyn_table>.

BREAK-POINT. " <b>Check contents of <dyn_table> in debugger</b>

END-OF-SELECTION.

&----


*& Form fb_table_create

&----


  • text

----


  • -->P_IT_FIELDS text

  • <--P_NEW_TABLE text

----


FORM fb_table_create "TABLES rt_fieldcat

CHANGING new_table.

  • DATA: ls_fieldcat TYPE TABLE OF dd03l WITH HEADER LINE.

DATA: l_name LIKE sy-repid.

DATA: l_message(240) TYPE c,

l_line TYPE i,

l_word(72) TYPE c.

DATA: BEGIN OF lt_source OCCURS 0,

line(128) TYPE c,

END OF lt_source.

DATA: l_form(30) TYPE c VALUE 'TABLE_CREATE'.

CONSTANTS:

con_report(60) TYPE c VALUE

'REPORT GENENERATED_SUBROUTINE_POOL.',

con_form(60) TYPE c VALUE

'FORM TABLE_CREATE CHANGING NEW_TABLE.',

con_tables(60) TYPE c VALUE

'TABLES :',

con_begin(60) TYPE c VALUE

'DATA: BEGIN OF LT_GENTAB OCCURS 0.',

con_end(60) TYPE c VALUE

'DATA: END OF LT_GENTAB.',

con_endform(60) TYPE c VALUE

'ENDFORM.',

con_reference(60) TYPE c VALUE

'DATA: POINTER TYPE REF TO DATA.',

con_create(60) TYPE c VALUE

'CREATE DATA POINTER LIKE LT_GENTAB[].'.

  • ls_fieldcat[] = rt_fieldcat[].

lt_source-line = con_report.

APPEND lt_source.

lt_source-line = con_form.

APPEND lt_source.

CONCATENATE con_tables pa_dbtab '.'

INTO lt_source-line SEPARATED BY space.

APPEND lt_source.

lt_source-line = con_begin.

APPEND lt_source.

  • LOOP AT rt_fieldcat INTO ls_fieldcat.

  • CLEAR lt_source-line.

  • CONCATENATE 'DATA:'

  • ls_fieldcat-fieldname

  • 'LIKE'

  • pa_dbtab

  • INTO lt_source-line SEPARATED BY space.

  • CONCATENATE lt_source-line '-' ls_fieldcat-fieldname '.'

*INTO

  • lt_source-line.

  • APPEND lt_source.

  • ENDLOOP.

CONCATENATE 'INCLUDE' 'STRUCTURE' pa_dbtab '.'

INTO lt_source-line SEPARATED BY space.

APPEND lt_source.

lt_source-line = con_end.

APPEND lt_source.

CLEAR lt_source-line.

lt_source-line = con_reference.

APPEND lt_source.

lt_source-line = con_create.

APPEND lt_source.

CONCATENATE 'NEW_TABLE' '=' 'POINTER.'

INTO lt_source-line

SEPARATED BY space.

APPEND lt_source.

lt_source-line = con_endform.

APPEND lt_source.

CATCH SYSTEM-EXCEPTIONS generate_subpool_dir_full = 9.

GENERATE SUBROUTINE POOL lt_source NAME l_name

MESSAGE l_message LINE l_line WORD l_word.

ENDCATCH.

CASE sy-subrc.

WHEN 0.

WHEN 9.

RAISE generate_subpool_dir_full.

WHEN OTHERS.

MESSAGE x000(0k) WITH l_message l_line l_word.

ENDCASE.

PERFORM (l_form) IN PROGRAM (l_name) CHANGING new_table.

ENDFORM. " FB_TABLE_CREATE

Read only

0 Likes
652

Sorry it didn't work

Read only

Former Member
0 Likes
654

Hello,

Check thsi.


type-pools: slis.
 
field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.
 
data: alv_fldcat type slis_t_fieldcat_alv,
      it_fldcat type lvc_t_fcat.
 
type-pools : abap.
 
data : it_details type abap_compdescr_tab,
       wa_details type abap_compdescr.
 
data : ref_descr type ref to cl_abap_structdescr.
 
data: new_table type ref to data,
      new_line  type ref to data,
      wa_it_fldcat type lvc_s_fcat.
 
selection-screen begin of block b1 with frame title text .
parameters: p_table(30) type c.
selection-screen end of block b1.
 
 
* Get the structure of the table.
ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details[] = ref_descr->components[].
 
loop at it_details into wa_details.
  clear wa_it_fldcat.
  wa_it_fldcat-fieldname = wa_details-name .
  wa_it_fldcat-datatype = wa_details-type_kind.
  wa_it_fldcat-inttype = wa_details-type_kind. 
  wa_it_fldcat-intlen = wa_details-length.
  wa_it_fldcat-decimals = wa_details-decimals.
  append wa_it_fldcat to it_fldcat .
endloop.
 
* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
             exporting
                it_fieldcatalog = it_fldcat
             importing
                ep_table        = new_table.
 
assign new_table->* to <dyn_table>.
 
* Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.
 
* Select Data from table.
select * into table <dyn_table>
           from (p_table).
 
* Write out data from table.
loop at <dyn_table> into <dyn_wa>.
  do.
    assign component  sy-index  of structure <dyn_wa> to <dyn_field>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ <dyn_field>.
    else.
      write: <dyn_field>.
    endif.
  enddo.
endloop.

REgards,

Vasanth

Read only

Former Member
0 Likes
652

Hi Lisa,

try this: