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: 

Difference between SELECT INTO CORRESPONDING and SELECT INTO(f1, f2…)?

luis_rod
Participant
0 Kudos

Hi,

(My apologies beforehand, newcomer to SAP)

I have a FORM that has the following (in pseudo_ode) statementes;

SELECT

  DB~F1

  DB~F2

  DB~F3

FROM DB1 JOIN DB2 ON .....

INTO CORRESPONDING FIELDS OF TABLE ITAB

WHERE (SELECT_CONDITIONS) .

later on (the same form) ...:

  LOOP AT ITAB  WHERE F1 <> ' '

    <some code here>

    SELECT SINGLE

      DB3~F4

      FROM DB3

      INTO ITAB-F4

      WHERE (DB~F1 = ITAB-F1).

    MODIFY gt_alv INDEX sy-tabix.

  ENDLOOP.

So far, so good.

Now, if I change the INTO CORRESPONDING BY

INTO

(ITAB-F1,

ITAB-F2)

I get the message:

Incorrect nesting: Before the statement "ENDFORM", the structure

introduced by "SELECT" must be concluded by "ENDSELECT" . . . . . . . .

Why is this so?

Thanks in advance,

Luis

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

you have changed the firs selection, haven't you?

If it's so you have to consider the option INTO CORRESPONDING FIELDS OF TABLE ....: in this case the SELECT doesn't need to close by ENDSE;ECT



So this selection:



SELECT

  DB~F1

  DB~F2

  DB~F3

FROM DB1 JOIN DB2 ON .....

INTO CORRESPONDING FIELDS OF TABLE ITAB

WHERE (SELECT_CONDITIONS) .

should be re-written:


SELECT

  DB~F1

  DB~F2

  DB~F3

FROM DB1 JOIN DB2 ON .....

INTO (ITAB-F1. ITAB-F2, ITAB-F3)

WHERE (SELECT_CONDITIONS)

    APPEND ITAB. <------------------------------------

ENDSELECT..      <-----------------------------------

11 REPLIES 11

Former Member
0 Kudos

Hi

you have changed the firs selection, haven't you?

If it's so you have to consider the option INTO CORRESPONDING FIELDS OF TABLE ....: in this case the SELECT doesn't need to close by ENDSE;ECT



So this selection:



SELECT

  DB~F1

  DB~F2

  DB~F3

FROM DB1 JOIN DB2 ON .....

INTO CORRESPONDING FIELDS OF TABLE ITAB

WHERE (SELECT_CONDITIONS) .

should be re-written:


SELECT

  DB~F1

  DB~F2

  DB~F3

FROM DB1 JOIN DB2 ON .....

INTO (ITAB-F1. ITAB-F2, ITAB-F3)

WHERE (SELECT_CONDITIONS)

    APPEND ITAB. <------------------------------------

ENDSELECT..      <-----------------------------------

0 Kudos

Max,

Thanks for your post.

Yes, I understand that ENDSELECT is needed (and yes, I forgot the APPEND :-[  )

My question is why this is needed? Why does the way you code the INTO have to make a difference?

My apologies if the question seems silly. My background is SQL programming and this seems  (to me, at least) a somewhat inconsistent (and inelegant) statement syntax...

Regards,

Luis

0 Kudos

The SELECT.....INTO....ENDSELECT is sought of a loop process. This syntax selects a single row into a work area and you can then append to an internal table.

The SELECT...INTO TABLE... is a single statement that works with multiple rows of a DB table.

So whenever you do not specify the INTO "TABLE" addition either you should explicitly state that you are selecting a single record (by using SELECT SINGLE) or you should use SELECT..ENDSELECT.

Hope this helps.

Thanks,

Vikram.M

0 Kudos

Vikram,

Thanks. I still think of it as a little bit inelegant 🙂 Maybe a SELECT ... INTO TABLE FIELDS(F, F2, F3) would be nicer.

[edited] Without ENDSELECT and/or APPEND...

Thanks again,

Luis

0 Kudos

Why would you want to specify the fields when you can achieve the same using the INTO CORRESPONDING OF TABLE?

Vikram.M

0 Kudos

Hello Luis,

The following syntax is not available yet


SELECT ... INTO TABLE FIELDS(F, F2, F3) would be nicer.

But if your SAP NW ABAP is 7.4 or higher you can write like the following way without declaring type-

select...from...into table @data(itab)

Thanks,

Mainak

0 Kudos

Hi

As Vikram has written SELECT command generally is a cycle, that means it needs to indicate where it starts (SELECT) and ends (ENDSELECT)

Of course you can insert all code you need between SELECT/ENDSELCT

The options like INTO TABLE allow to upload the selection in one shot, so it doesn't need to close the SELECT by ENDSELECT

I suppose it needs to consider the internal table is defined by two elements:

- the headerline (here you can work on its structure)

- the content, the data

There are many ways to define an internal table with or without headerline, for example:



DATA ITAB LIKE/TYPE STANDARD TABLE OF <.................> WITH HEADER LINE.

DATA ITAB LIKE/TYPE STANDARD TABLE OF <.................>.


Which kind definition to be used depends on what you need to use

but anyway you can move the data in the headerline and append it to the content

So you can write:


MOVE: ....... TO ITAB-FIELD1,

                    TO ITAB-FILED2,

                    TO ITAB-FIELDN.

APPEND ITAB.

And not something like:


MOVE: ....... APPEND  ITAB-FIELD1, ITAB-FILED2,...ITAB-FIELDN),

I suppose that the commands for the inner table can work on a single element : headerline or content , because the headerline can know as the single line is structured , the content can't know it : is just the set of data.

That's valid for options in the SELECT command:

- Headerline


SELECT FIELD1 FIELD2......FIELDN INTO (ITAB-FIELD1, ITAB-FIELD2,...., ITAB-FIELDN)

- Content


SELECT FIELD1 FIELD2......FIELDN INTO TABLE ITAB

Of course this is my opinion, but I hope it can be helpfull for you

Max

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

If you are a newcomer to SAP, coming from SQL, you have two new things to understand in your example:

First, the ITAB in your example seems to be an internal table with an obsolete header line. This is an old and rather ugly concept where two data objects - a structured work area and the internal table itself - have the same name. Only this fact allows you to write INTO (ITAB-F1, ITAB-F2) at all! In this case the header line and not the tabular internal table is addressed! The example would be more clear with an internal table without header line.

Second, you have to understand how the (as a rule always) tabular result of a DB query is assigned to an ABAP data object.  This is governed by the ABAP specific INTO clause. In the ABAP documentation of SELECT you find the rules when a (ABAP specific) loop is opened that has to be closed by ENDSELECT and when the tabular result is copied directly. The latter is the case for tabular target areas (internal tables) and also when using the ABAP specific addition SINGLE . There are also some peculiarities for aggregates in the SELECT list.

Horst

0 Kudos

Horst,

Thanks for your reply. There are some things about SAP that still baffle me, even when I read the docs... 🙂

Thanks again,

Luis

Sandra_Rossi
Active Contributor
0 Kudos

If you are a newcomer to SCN, please read the forum engagement rules (please search them too), i.e. you should search because it is explained in the forum and in the abap documentation.

former_member188843
Participant
0 Kudos

please read abap document where u could find the reason. U need know the basic before u go deep to avoid lost.

ABAP Keyword Documentation