<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Dynamic select statement in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamic-select-statement/m-p/3429052#M823681</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unfortunately -- it's not not quite that simple although still easy enough.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In order to use a dynamic table you need to create a field catalog first for the fields, build the dynamic table and then populate it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's how to do it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) define some data we'll need for the process.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

* Define any structure
TYPES:  BEGIN OF s_elements,
  vbeln   TYPE vapma-vbeln,
  posnr   TYPE vapma-posnr,
  matnr   TYPE vapma-matnr,
  kunnr   TYPE vapma-kunnr,
  werks   TYPE vapma-werks,
  vkorg   TYPE vapma-vkorg,
  vkbur   TYPE vapma-vkbur,
  status  TYPE c,

END OF  s_elements.

* end of your structure
DATA:wa_elements TYPE s_elements.
DATA: ord_nr TYPE vapma-vbeln,
      mat_nr TYPE vapma-matnr,
      cus_nr TYPE vapma-kunnr.


DATA lr_rtti_struc TYPE REF TO cl_abap_structdescr .
DATA:
zog                     LIKE LINE OF lr_rtti_struc-&amp;gt;components .
DATA:
zogt                    LIKE TABLE OF zog,
wa_it_fldcat TYPE lvc_s_fcat,
it_fldcat TYPE lvc_t_fcat ,
dy_line            TYPE REF TO data,
dy_table           TYPE REF TO data.


DATA:  dref               TYPE REF TO data.
FIELD-SYMBOLS: &amp;lt;fs&amp;gt; TYPE ANY,
   &amp;lt;dyn_table&amp;gt;    TYPE  STANDARD TABLE,
   &amp;lt;dyn_wa&amp;gt;.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2)  now build the fcat, create the dynamic table and populate it.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
START-OF-SELECTION.
*now I want to build a field catalog
* First get your data structure into a field symbol

  CREATE DATA dref TYPE s_elements.
  ASSIGN dref-&amp;gt;* TO &amp;lt;fs&amp;gt;.

  lr_rtti_struc ?= cl_abap_structdescr=&amp;gt;describe_by_data( &amp;lt;fs&amp;gt; ).

* Now get the structure details into a table.
* table zogt[] contains the structure details
* From which we can build the field catalog

  zogt[]  = lr_rtti_struc-&amp;gt;components.
  LOOP AT zogt INTO zog.
    CLEAR wa_it_fldcat.
    wa_it_fldcat-fieldname = zog-name .
    wa_it_fldcat-datatype = zog-type_kind.
    wa_it_fldcat-inttype = zog-type_kind.
    wa_it_fldcat-intlen = zog-length.
    wa_it_fldcat-decimals = zog-decimals.
    wa_it_fldcat-coltext = zog-name.
    wa_it_fldcat-lowercase = 'X'.
    IF wa_it_fldcat-fieldname = 'VBELN'.
      wa_it_fldcat-hotspot = 'X'.
    ENDIF.
    APPEND wa_it_fldcat TO it_fldcat.

  ENDLOOP.
*
* You can perform any modifications / additions to your field catalog
* here such as your own column names etc.

* Now using the field catalog created above we can
* build a dynamic table
* and populate it

* First build the dynamic table
* the table will contain entries for
* our structure defined at the start of the program

  CALL METHOD cl_alv_table_create=&amp;gt;create_dynamic_table
         EXPORTING
              it_fieldcatalog = it_fldcat
         IMPORTING
              ep_table = dy_table.


  ASSIGN dy_table-&amp;gt;* TO &amp;lt;dyn_table&amp;gt;.
  CREATE DATA dy_line LIKE LINE OF &amp;lt;dyn_table&amp;gt;.
  ASSIGN dy_line-&amp;gt;* TO &amp;lt;dyn_wa&amp;gt;.

* Now fill our table with data

  SELECT vbeln posnr matnr kunnr werks vkorg vkbur
         UP TO 200 ROWS
         FROM vapma
         INTO  CORRESPONDING FIELDS OF TABLE &amp;lt;dyn_table&amp;gt;.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3)  now if you want to say use a "classical" table  all you need to do is&lt;/P&gt;&lt;P&gt;define a standard table of type s_elements and code something simple like&lt;/P&gt;&lt;P&gt; my_itab[] =  &amp;lt;dyn_table&amp;gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then you could get the &amp;lt;dyn_table&amp;gt;  after creation into yoour own table  for further processing.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can also  display the &amp;lt;dyn_table&amp;gt;  in a grid simply  via some type of code such as &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
 CALL METHOD grid1-&amp;gt;set_table_for_first_display
    EXPORTING is_layout =  struct_grid_lset
    CHANGING
               it_outtab       = &amp;lt;dyn_table&amp;gt;
               it_fieldcatalog = it_fldcat.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You'll need to  "instantiate" the class (have grid1 as a ref to cl_gui_alv_grid) and do some other work --that's a topic for another post - however since you now have the field catalog and the table in principle if you need a grid display you've done 90% of the work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cheers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;jimbo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 14 Feb 2008 13:11:53 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2008-02-14T13:11:53Z</dc:date>
    <item>
      <title>Dynamic select statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamic-select-statement/m-p/3429051#M823680</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have this input field called IO_TABLE and a button. When the user key in the database name into IO_TABLE and press the button, the records/data in the database will be written out.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is my codes:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
REPORT  ZA_FIELD.
DATA IO_TABLE(30) TYPE C.

FIELD-SYMBOLS:  &amp;lt;FS_INPUT&amp;gt; TYPE ANY,
               &amp;lt;FS_ITAB&amp;gt; TYPE ANY TABLE.


START-OF-SELECTION.
  CALL SCREEN 9000.


MODULE STATUS_9000 OUTPUT.
SET PF-STATUS 'SCREEN_9000'.
SET TITLEBAR 'TITLE_9000'.

ENDMODULE.                    "STATUS_9002 OUTPUT

MODULE USER_COMMAND_9000 INPUT.

IF SY-UCOMM = 'SAVE'.

ELSEIF SY-UCOMM = 'EXIT' OR SY-UCOMM = 'CANCEL'.
  LEAVE PROGRAM.
ELSEIF SY-UCOMM = 'PUSH'.
  ASSIGN IO_TABLE TO &amp;lt;FS_INPUT&amp;gt;.

  SELECT * FROM &amp;lt;FS_INPUT&amp;gt; INTO TABLE &amp;lt;FS_ITAB&amp;gt;.
  WRITE &amp;lt;FS_ITAB&amp;gt; .
"  SUBMIT ZFINAL_2.
ENDIF.

ENDMODULE.                    "USER_COMMAND_9002 INPUT
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As shown &amp;lt;FS_INPUT&amp;gt; is the field symbol that store the value of IO_TABLE. What am I suppose to replace my dynamic internal table for my codes?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;dynamic internal table is needed to display out the values in it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I got this error at my statement as shown at my link &lt;A href="http://img337.imageshack.us/img337/3791/screenez4.png" target="test_blank"&gt;http://img337.imageshack.us/img337/3791/screenez4.png&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What am I suppose to do to solve this problem?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Feb 2008 09:55:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/dynamic-select-statement/m-p/3429051#M823680</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-02-14T09:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic select statement</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/dynamic-select-statement/m-p/3429052#M823681</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unfortunately -- it's not not quite that simple although still easy enough.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In order to use a dynamic table you need to create a field catalog first for the fields, build the dynamic table and then populate it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's how to do it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1) define some data we'll need for the process.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

* Define any structure
TYPES:  BEGIN OF s_elements,
  vbeln   TYPE vapma-vbeln,
  posnr   TYPE vapma-posnr,
  matnr   TYPE vapma-matnr,
  kunnr   TYPE vapma-kunnr,
  werks   TYPE vapma-werks,
  vkorg   TYPE vapma-vkorg,
  vkbur   TYPE vapma-vkbur,
  status  TYPE c,

END OF  s_elements.

* end of your structure
DATA:wa_elements TYPE s_elements.
DATA: ord_nr TYPE vapma-vbeln,
      mat_nr TYPE vapma-matnr,
      cus_nr TYPE vapma-kunnr.


DATA lr_rtti_struc TYPE REF TO cl_abap_structdescr .
DATA:
zog                     LIKE LINE OF lr_rtti_struc-&amp;gt;components .
DATA:
zogt                    LIKE TABLE OF zog,
wa_it_fldcat TYPE lvc_s_fcat,
it_fldcat TYPE lvc_t_fcat ,
dy_line            TYPE REF TO data,
dy_table           TYPE REF TO data.


DATA:  dref               TYPE REF TO data.
FIELD-SYMBOLS: &amp;lt;fs&amp;gt; TYPE ANY,
   &amp;lt;dyn_table&amp;gt;    TYPE  STANDARD TABLE,
   &amp;lt;dyn_wa&amp;gt;.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2)  now build the fcat, create the dynamic table and populate it.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
START-OF-SELECTION.
*now I want to build a field catalog
* First get your data structure into a field symbol

  CREATE DATA dref TYPE s_elements.
  ASSIGN dref-&amp;gt;* TO &amp;lt;fs&amp;gt;.

  lr_rtti_struc ?= cl_abap_structdescr=&amp;gt;describe_by_data( &amp;lt;fs&amp;gt; ).

* Now get the structure details into a table.
* table zogt[] contains the structure details
* From which we can build the field catalog

  zogt[]  = lr_rtti_struc-&amp;gt;components.
  LOOP AT zogt INTO zog.
    CLEAR wa_it_fldcat.
    wa_it_fldcat-fieldname = zog-name .
    wa_it_fldcat-datatype = zog-type_kind.
    wa_it_fldcat-inttype = zog-type_kind.
    wa_it_fldcat-intlen = zog-length.
    wa_it_fldcat-decimals = zog-decimals.
    wa_it_fldcat-coltext = zog-name.
    wa_it_fldcat-lowercase = 'X'.
    IF wa_it_fldcat-fieldname = 'VBELN'.
      wa_it_fldcat-hotspot = 'X'.
    ENDIF.
    APPEND wa_it_fldcat TO it_fldcat.

  ENDLOOP.
*
* You can perform any modifications / additions to your field catalog
* here such as your own column names etc.

* Now using the field catalog created above we can
* build a dynamic table
* and populate it

* First build the dynamic table
* the table will contain entries for
* our structure defined at the start of the program

  CALL METHOD cl_alv_table_create=&amp;gt;create_dynamic_table
         EXPORTING
              it_fieldcatalog = it_fldcat
         IMPORTING
              ep_table = dy_table.


  ASSIGN dy_table-&amp;gt;* TO &amp;lt;dyn_table&amp;gt;.
  CREATE DATA dy_line LIKE LINE OF &amp;lt;dyn_table&amp;gt;.
  ASSIGN dy_line-&amp;gt;* TO &amp;lt;dyn_wa&amp;gt;.

* Now fill our table with data

  SELECT vbeln posnr matnr kunnr werks vkorg vkbur
         UP TO 200 ROWS
         FROM vapma
         INTO  CORRESPONDING FIELDS OF TABLE &amp;lt;dyn_table&amp;gt;.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3)  now if you want to say use a "classical" table  all you need to do is&lt;/P&gt;&lt;P&gt;define a standard table of type s_elements and code something simple like&lt;/P&gt;&lt;P&gt; my_itab[] =  &amp;lt;dyn_table&amp;gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then you could get the &amp;lt;dyn_table&amp;gt;  after creation into yoour own table  for further processing.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can also  display the &amp;lt;dyn_table&amp;gt;  in a grid simply  via some type of code such as &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
 CALL METHOD grid1-&amp;gt;set_table_for_first_display
    EXPORTING is_layout =  struct_grid_lset
    CHANGING
               it_outtab       = &amp;lt;dyn_table&amp;gt;
               it_fieldcatalog = it_fldcat.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You'll need to  "instantiate" the class (have grid1 as a ref to cl_gui_alv_grid) and do some other work --that's a topic for another post - however since you now have the field catalog and the table in principle if you need a grid display you've done 90% of the work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cheers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;jimbo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Feb 2008 13:11:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/dynamic-select-statement/m-p/3429052#M823681</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-02-14T13:11:53Z</dc:date>
    </item>
  </channel>
</rss>

