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

Creating and running Query dynamically.

Former Member
0 Likes
682

Hi Gurus,

This is my first post here with a hope that I will get my problem resolved.

I want to create a query to give me internal table using the table name. This logic will be used for some other work I have to accomplish.

I am selecting the field names from the table DD03L for my Z-table.


SELECT * FROM dd03l into corresponding fields of table mytable
WHERE tabname = 'ZMS_MCLINT'.
sort mytable by position.

Again, based on the fields selected I will create a select query


QueryStr = 'Select'.
assign QueryStr to <fs>.
Loop at mytable.
  if not mytable-fieldname(1) = '.'.
    concatenate <fs> mytable-fieldname into <fs> separated by SPACE .
  endif.
endloop.
concatenate <fs> 'from ' mytable-fieldname 'into table itab.' into <fs> separated by SPACE .

This will give me the result as


Select MANDT CLCODE CLNAME ADDRESS1 ADDRESS2 ADDRESS3 AREA CITY PINCODE from PINCODE into table itab

Now My requirement is to execute the query which i have build on the fly to get the data into iTAB. How can I execute the query which is assigned in field symbol <FS>.

If there is any other logic also its welcome.

Thanks in advance.

Regards

Munir Solkar (MS)

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
648

Hi,

check this link.

https://www.sdn.sap.com/irj/scn/wiki?path=/display/snippets/creatingFlatandComplexInternalTablesDynamicallyusingRTTI

regards,

Santosh Thorat

6 REPLIES 6
Read only

Former Member
0 Likes
649

Hi,

check this link.

https://www.sdn.sap.com/irj/scn/wiki?path=/display/snippets/creatingFlatandComplexInternalTablesDynamicallyusingRTTI

regards,

Santosh Thorat

Read only

Former Member
0 Likes
648

Dear +Santosh,+

Thanks for the quick response.

That was really helpful.

Regards

MS

Read only

Former Member
0 Likes
648

Dear Friends,

The above link is applicable for only a single table. What if we have to extract fields dynamically from two different tables.

my requirement is as follows.

I need to display fields from VBAK and VBAP tables with relation as VBAK-VBELN = VBAP-VBELN.

A separate table is created Z_IF_FIELDS to store the columns required to be displayed for a particular report.

The data is as follows:


Report Name           Position          Column Name
SO_REPORT              1                 VBAK-VBELN
SO_REPORT              2                 VBAP-POSNR
SO_REPORT              3                 VBAP-MATNR
SO_REPORT              4                 VBAP-KWMENG

The program should fetch only above fields from two tables mentioned (vbak, vbap) in an internal table and display the result as an output to the user (either list view and ALV).

Kindly help at the earliest.

Regards

MS

Read only

Former Member
0 Likes
648

Need help for multiple tables

Read only

Former Member
0 Likes
648

Hi...

Instead of forming the whole query through concatenation, concatenate only the fieldnames.

Then pass it in the query as follows:

Loop at mytable.

if not wa_mytable-fieldname(1) = '.'.

concatenate <fields> wa_mytable-fieldname into fields separated by SPACE .

endif.

endloop.

select <fields> from into into table itab .

Hope it helps you.

Regards,

K.Tharani.

Read only

Former Member
0 Likes
648

Hi,

Store your filed names separately then use it like,

select (fileds) from (table) into table itab.

Regards,

Manoj Kumar P