Select statements are used to fetch data from the database.
A simple statement can be written as
SELECT SINGLE * FROM MARA .
(MARA - Material Master Table)
If we need to give some condition, we can alter the query as
SELECT SINGLE * FROM MARA WHERE MATNR EQ ‘T-SHIRT’.
In case if you need to select more than one row, we should write the select statement
In SELECT …. END SELECT block
SELECT * FROM MARA INTO ITAB
WHERE MATNR LIKE ‘T-SHIRT%’.
* Here we should do the processing of the records fetched.
* Eg : APPEND ITAB
END SELECT.
ITAB Is the table declared of type MARA. Please note it should be with header line.
DATA : itab TYPE STANDARD TABLE OF mara WITH HEADER LINE.
So the program can be written as
*************************************************************
REPORT ZABAPTEST.
DATA : itab TYPE STANDARD TABLE OF mara WITH HEADER LINE.
SELECT * FROM mara INTO itab
WHERE matnr LIKE 'LN%'.
APPEND itab.
ENDSELECT.
LOOP AT itab.
WRITE : 'Matnr ', itab-matnr.
ENDLOOP.
*************************************************************
Now there is a second method of fetching via ARRAY FETCH. Which means that
All the database records will be fetched all together
SELECT * FROM MARA
INTO TABLE ITAB
WHERE MATNR LIKE ‘LN%’.
Note that there is no END SELECT block.
And the program can be written as
*************************************************************
REPORT ZABAPTEST.
DATA : itab TYPE STANDARD TABLE OF mara WITH HEADER LINE.
SELECT * FROM mara INTO TABLE itab
WHERE matnr LIKE 'LN%'.
LOOP AT itab.
WRITE : 'Matnr ', itab-matnr.
ENDLOOP.
*************************************************************
Of these two select statements, always try to use the second select statement. The first select statement will keep the database cursor open for a longer time till all the records are fetched from the database. The second select statement will do a single call to the database and in one stretch fetches all the records.
No comments:
Post a Comment