Skip to Content
author's profile photo Former Member
Former Member

How to dynamic create Select SQL statements.

Hi frnds,

I want to make a SELECT statement in which all values I will get dynamically. Like i have two fields on selection screen, one for table name and one for field name. Now I need to make my ITAB according to the table selected and field name selected. So finally my ITAB will be having the field which I chosen from selection screen.

Now how can I make a dynamic ITAB and how to create a dynamic SELECT statement?

It wud be great if you can help me.



Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Aug 11, 2005 at 08:16 PM

    Here is a sample program....

    This will get you started in programming a dynamic internal table as well as dynamic select.

    report zrich_0002.
    type-pools: slis.
    field-symbols: <dyn_table> type standard table,
    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-intlen = wa_details-length.
      wa_it_fldcat-decimals = wa_details-decimals.
      append wa_it_fldcat to it_fldcat .
    * Create dynamic internal table and assign to FS
    call method cl_alv_table_create=>create_dynamic_table
                    it_fieldcatalog = it_fldcat
                    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 corresponding fields of table <dyn_table>
               from (p_table).
    * Write out data from table.
    loop at <dyn_table> into <dyn_wa>.
        assign component  sy-index  of structure <dyn_wa> to <dyn_field>.
        if sy-subrc <> 0.
        if sy-index = 1.
          write:/ <dyn_field>.
          write: <dyn_field>.


    Rich Heilman

    Message was edited by: Rich Heilman

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 11, 2005 at 08:27 PM

    Hi Arpit

    Please check 'Using dynamic SELECT statement' in the following link. faqs.faq

    I found it very interesting. Hope it will help you.

    Ashish Jain

    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.