cancel
Showing results for 
Search instead for 
Did you mean: 

Select into specified fields

Former Member
0 Kudos
18,476

In the select statement, how can I specify fields of an internal table which I want to be filled from a database table? Is it possible to define in INTO clause that I want db_field1 to be copied into itab_field3?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

... INTO (f1, ..., fn)

Places the result set in the target area (f1, ..., fn). The fields of the result set are transported to the target fields fi from left to right. INTO (f1, ..., fn) is allowed only if a list with n elements is also specified in the SELECT clause.

If the result of a selection is a table, the data is retrieved in a processing loop introduced by SELECT and concluded by ENDSELECT. The processing passes through the loop once for each line read. If the result is a single record, the closing ENDSELECT is omitted.

Example

Output a list of all airlines (with short description and name):

TABLES SCARR.

DATA: CARRID LIKE SCARR-CARRID,

CARRNAME LIKE SCARR-CARRNAME,

SELECT CARRID CARRNAME

INTO (CARRID, CARRNAME)

FROM SCARR.

WRITE: / CARRID, CARRNAME.

ENDSELECT

If u have defined an internal tabel and in select query u r retrieving a few fields then use INTO CORRESPONDING FIELDS OF..

DATA: Begin of tab occurs 0,

matnr like mara-matnr,

maktl like mara maktl,

end of tab.

select matnr from

mara <b>into corresponding fields of table tab</b>

where matnr IN s_matnr.

Try this one.

Message was edited by: Judith Jessie Selvi

Former Member
0 Kudos

The database table and internal table are not the same.

Select - Endselect is a fine solution. Thanks.

Answers (8)

Answers (8)

Former Member
0 Kudos

well jagath is true, but always avoid joins.

this can cost you performance. it is ok to use joins if database table are small. but general rule is try to avoid them as much as possible.

Former Member
0 Kudos

Hi,

there are numerous ways of inserting DB table entries into your internal table fields. just explain you with a simple example:

Data : Begin of i_flight occurs 0,

carrid like sflight-carrid,

connid like sflight-connid,

fldate like sflight-fldate,

cityfrom like spfli-cityfrom,

countryfrom like spfli-countryfrom,

carrname like scarr-carrname,

end of i_flight.

here the internal table has fields from three different table sflight, spfli, scarr which are linked by a common field carrid. now using this you can select the entries from these tables into your specific fields of your internal table.

select a~carrid

b~connid

b~fldate

c~cityfrom

c~countryfrom

a~carrname

into table i_flight

from scarr as a inner join spfli as b

on acarrid = bcarrid inner join sflight as c

on acarrid = ccarrid

where a~carrid = 'LH'.

if sy-subrc ne 0.

endif.

regarding your query :Is it possible to define in INTO clause that I want db_field1 to be copied into itab_field3?

the answer would be like if you are going to insert entries for the first time using that select clause then it is possible, but if you are going to use that clause for an internal table which already has got entries in it and using for all entries option then it will not work as it will write into itab_field3 but overwrites the other table field values. if you are going to use this select query inside loop endloop statement then it will work but it is advisable to use that methodology as it will decrease the programming efficiency.

Regards,

jagath.

Former Member
0 Kudos

Hey,

Then according to my case 2, it should work right ???

Select and End select is not necessary.

Just,

Select (f1,f2,,,) from db table into corresponding fields of <internal table> will work fine.

Cheers,

Venkat

Former Member
0 Kudos

Hi,

Case 1:

========

If you declared a internal table of same type as data base table with same fields, then

Select * from db table in to <internal table> will work fine.

Case 2:

======

But , if you declared a internal table contaiing only specific fields as in db table and with extra fields which are not in db table, then

Select f1,f2,f3 (or) * from db table in to corresponding fields of <internal table>.

Do inform me if this works.

This will work perfectly.

Cheers,

Venkat

Former Member
0 Kudos

Hi Mikola,

Did u check my 2 cases mentioned.I think for you the case 2 will suit.

Do inform me if it works fine.

Regs,

Venkat

Former Member
0 Kudos

hi mikola,

try this

DATA: BEGIN OF it_tab OCCURS 0,

test LIKE mara-matnr,

END OF it_tab.

SELECT matnr AS test INTO CORRESPONDING FIELDS OF TABLE it_tab

FROM mara

WHERE matnr = 'put yout material number here'.

you will see in table it_Tab the field test will contain the value of mara-matnr.

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi Mikola,

For your requirement,you can use the following.

select field1 field2 from database <b>into corresponding fields of table</b> internal_table.

Former Member
0 Kudos

Hi,

Case 1:

========

If you declared a internal table of same type as data base table with same fields, then

Select * from db table in to <internal table> will work fine.

Case 2:

======

But , if you declared a internal table contaiing only specific fields as in db table and with extra fields which are not in db table, then

Select f1,f2,f3 (or) * from db table in to corresponding fields of <internal table>

This will work perfectly.

Cheers,

Venkat

Former Member
0 Kudos

Hi,

1) To move into only certain fields certain values using the select statement,

eg:

SELECT MATNR FROM MARA INTO ITAB-MATNR

WHERE...

2) But in case you want to select the entire table contents (all coloumns) into the internal table having the same structure as that of the transparent table, then use

SELCT * FROM MARA INTO TABLE ITAB.

3) Incase, you wanna select multiple cols into corresponding fields of table itab,

then

SELECT * FROM MARA INTO CORRESPONDING FIELDS OF TABLE ITAB.

Cheers,

Sam