Application Development 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: 

Dynamic SQL in abap

former_member1302095
Participant
0 Kudos

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.

6 REPLIES 6

Former Member
0 Kudos

Use for all entries query.

rosenberg_eitan
Active Contributor
0 Kudos

Hi,

See if this article from IBM can help.

Using dynamic Open SQL to perform joins on SAP transparent tables

Regards.

himanshu_gupta13
Employee
Employee
0 Kudos

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

VenkatRamesh_V
Active Contributor
0 Kudos

Hi,

Use Field symbols.

Regards,

Venkat

former_member196651
Contributor
0 Kudos

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

former_member202818
Active Contributor
0 Kudos

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