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

Dynamic table

0 Likes
1,912

Hi,

I have created a dynamic table and i have given SELECT * FROM … Up to 20 rows and i got the data of 20 rows of the selected table but i only want first five fields how can i get it.

Regards,

Prateex

8 REPLIES 8
Read only

lenastodal
Product and Topic Expert
Product and Topic Expert
0 Likes
1,814

Thank you for visiting SAP Community to get answers to your questions. Since you're asking a question here for the first time, I recommend that you familiarize yourself with: https://community.sap.com/resources/questions-and-answers, as it provides tips for preparing questions that draw responses from our members.
For example, you can:
- outline what steps you took to find answers (and why they weren't helpful)
- share screenshots of what you've seen/done
- make sure you've applied the appropriate tags
- use a more descriptive subject line

The more details you provide, the more likely it is that members will be able to respond. Feel free to also take our Q&A tutorial at: https://developers.sap.com/tutorials/community-qa.html. Should you wish, you can revise your question by selecting Actions, then Edit. By adding a picture to your profile you encourage readers to respond: https://www.youtube.com/watch?v=46bt1juWUUM


Join or subscribe to SAP Community Groups to stay up-to-date, including SAP TechEd Group.
Read only

Preetha33
Explorer
1,814

Hi prateex,

Select required fields which we want to fetch from table we will get required fields data from table.

 data: lt_mara type STANDARD TABLE OF mara,
ls_mara type mara.

select matnr
mbrsh
matkl
mtart
from mara into CORRESPONDING FIELDS OF table lt_mara UP TO 20 rows.

write: 'Material Number',
'Industry Sector',
'Material Group',
'Material type'.

loop at lt_mara into ls_mara.
write: / ls_mara-matnr,
ls_mara-mbrsh,
ls_mara-matkl,
ls_mara-mtart.
endloop.

Now, we will get only required fields from table and 20rows of data.

Reward points if helpful,

Thanks & Regards,

Preetha

Read only

0 Likes
1,814

hi preetha,

here the table is dynamic we don't know which table we will use to fetch data, we enter table name in selection screen .

so we don't know the field names then how can we write them in select query.

Read only

FredericGirod
Active Contributor
0 Likes
1,814

check the FM RFC_READ_TABLE

Read only

ThorstenHoefer
Active Contributor
1,814

Hi Shiva,

please try this:

DATA: lr_tab_ref TYPE REF TO data,
      lr_struct  TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS: <lt_tab> type any table.

lr_struct ?= cl_abap_structdescr=>describe_by_name( 'SFLIGHTS' ).

DATA(lt_comp) = lr_struct->get_components( ).

delete lt_comp from 6.

lr_struct = cl_abap_structdescr=>create( p_components = lt_comp ).


DATA(lr_tab) = cl_abap_tabledescr=>create( p_line_type  = lr_struct ).
CREATE DATA lr_tab_ref TYPE HANDLE lr_tab.
ASSIGN LR_TAB_REF->* to <lt_tab>.
SELECT *
  FROM ('SFLIGHTS')
  INTO CORRESPONDING FIELDS OF table @<lt_tab>
  up to 20 rows.
Read only

0 Likes
1,814

With SELECT * and a dynamic table (FROM (tablename)), although there's "into corresponding fields", is the SELECT sent to the database really limited to only the components of the internal table?

Read only

0 Likes
1,814

Hi Sandra,

this is a working example.I have tried it.

Best Regards

Thorsten

Read only

1,814

I didn't doubt that your code works fine, I was asking whether the SAP Database Interface does optimize the SQL request sent to the database.

Just checked with 7.52 + kernel 753 + S/4HANA, it successfully selects only the 5 columns which are in the internal table:

SELECT
  /* FDA READ */
  "MANDT" , "CARRID" , "CARRNAME" , "CONNID" , "COUNTRYFR"
FROM
  "SFLIGHTS"
WHERE
  "MANDT" = ?
LIMIT  20
  WITH RANGE_RESTRICTION('CURRENT')