Skip to Content

Dynamic SQL in abap

i have a requirement to retrieve only selected fields from db table. i have to use this code with jco, so i can't go with ALV. my requirement is as follows,

list of fields from mara and marc table will be shown as checkbox and user will be allowed to select the required fields. that selected fields only retrieved from the table. please gimme some idea.

Thanks in advance.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Posted on Jun 09, 2014 at 09:10 AM

    Hi Remya,

    The fields that need to be selected can be given in an internal table and can be called in the Open SQL statements. Following is a sample code. You can modify this as you required.

    TABLES ekko.

    TYPES : BEGIN OF TY_FIELDS,

    FNAME TYPE LVC_FNAME.

    TYPES : END OF TY_FIELDS.

    DATA : GT_FIELDS type table of TY_FIELDS,

    WA_FIELDS like line of GT_FIELDS.

    DATA : lt_ekko TYPE TABLE OF ekko.

    SELECT-OPTIONS s_ebeln FOR ekko-ebeln.

    CLEAR WA_FIELDS.

    WA_FIELDS-FNAME = 'EBELN'.

    APPEND WA_FIELDS TO IT_FIELDS.

    CLEAR WA_FIELDS.

    WA_FIELDS-FNAME = 'BUKRS'.

    APPEND WA_FIELDS TO IT_FIELDS.

    CLEAR WA_FIELDS.

    WA_FIELDS-FNAME = 'BSTYP'.

    APPEND WA_FIELDS TO IT_FIELDS.

    SELECT (GT_FIELDS) FROM ekko INTO CORRESPONDING FIELDS OF TABLE lt_ekko

    WHERE ebeln IN s_ebeln.

    Based on your requirement, you need to modify your code.

    Regards,

    Abijith

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 09, 2014 at 08:17 AM

    Use for all entries query.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 09, 2014 at 08:29 AM

    Hi,

    See if this article from IBM can help.

    Using dynamic Open SQL to perform joins on SAP transparent tables

    Regards.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 09, 2014 at 08:42 AM

    Hii



    data : lv_tab(8) type c ,
    lv_from(200) type c ,
    lv_where(1024) type c ,

    lv_fld(200) type c,
    lv_temp_kbetr type konp-kbetr .



    concatenate 'mara' into lv_tab .
    concatenate lv_tab ' inner join marc on ' into lv_from separated by
    space .
    concatenate 'mara~matnr = marc~matnr' into lv_from .


    loop at itab where chk = 'X'.

    concatenate lv_fld itab-fld into lv_fld.

    endloop.

    select single (lv_fld)
    into lv_temp_kbetr
    from (lv_from)
    where WERKS = <input>.


    Kindly check the code above, it would be like that.


    Many Thanks/ HImanshu Gupta

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 09, 2014 at 09:02 AM

    Hi,

    Use Field symbols.

    Regards,

    Venkat

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 09, 2014 at 09:47 AM

    HI Remya,

    Try this..

    TYPES: BEGIN OF STYPE_FIELDS,

    FIELDNAME TYPE NAME_FELD,

    END OF STYPE_FIELDS.

    DATA: G_T_MSEG_FIELDS TYPE TABLE OF STYPE_FIELDS,

    G_S_MSEG_FIELDS TYPE STYPE_FIELDS.


    G_S_MSEG_FIELDS-FIELDNAME = 'BUSTM'.

    APPEND G_S_MSEG_FIELDS TO G_T_MSEG_FIELDS.

    G_S_MSEG_FIELDS-FIELDNAME = 'BWART'.

    APPEND G_S_MSEG_FIELDS TO G_T_MSEG_FIELDS.

    **Similarly u can modify the internal table with required fields to fetch according to ur requiremen

    SELECT (g_t_mseg_fields)

    INTO CORRESPONDING FIELDS OF TABLE g_t_mseg_lean

    FROM mseg.

    Regards

    Sreekanth

    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.