2022 Feb 28 8:01 AM
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
2022 Feb 28 8:02 AM
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
2022 Feb 28 8:49 AM
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
2022 Feb 28 11:56 AM
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.
2022 Feb 28 9:02 AM
2022 Feb 28 1:01 PM
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.
2022 Feb 28 3:05 PM
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?
2022 Mar 01 6:58 AM
Hi Sandra,
this is a working example.I have tried it.
Best Regards
Thorsten
2022 Mar 01 8:44 AM
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')