100% found this document useful (3 votes)
5K views4 pages

Different Select Statements in SAP ABAP

The document discusses different types of SELECT statements that can be used in SAP ABAP including SELECT *, SELECT SINGLE, SELECT MAX, SELECT MIN, SELECT UP TO, SELECT DISTINCT, SELECT ORDER BY, and using wildcards. It provides the syntax for each statement and examples of how to use them to select data from database tables in SAP ABAP programs.

Uploaded by

Mahmoud Diab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
5K views4 pages

Different Select Statements in SAP ABAP

The document discusses different types of SELECT statements that can be used in SAP ABAP including SELECT *, SELECT SINGLE, SELECT MAX, SELECT MIN, SELECT UP TO, SELECT DISTINCT, SELECT ORDER BY, and using wildcards. It provides the syntax for each statement and examples of how to use them to select data from database tables in SAP ABAP programs.

Uploaded by

Mahmoud Diab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Different Select Statements in SAP ABAP

 Step1 : Using Select * in SAP ABAP


 Step2 : Using Select Single in SAP ABAP
 Step3 : Using Select Max in SAP ABAP
 Step4 : Using Select Min in SAP ABAP
 Step5 : Using Select UP TO in SAP ABAP
 Step6 : Using Select Distinct in SAP ABAP
 Step7 : Using Select Order by in SAP ABAP
 Step8 : Using Wildcards in Selects

Step1:Using Select * in SAP ABAP Important Top^


Select * is a statement which is used to read whole data from a database table. The below is the example
code for reading data from MARA table.

**Declare internal table


DATA : IT_MARA TYPE TABLE OF MARA.

*use select * to read data


SELECT * FROM MARA INTO TABLE IT_MARA .

The below example code is used to get data based on a condition with all fields(columns) from MARA
table.

**Declare internal table


DATA : IT_MARA TYPE TABLE OF MARA.

* read data
SELECT * FROM MARA INTO TABLE IT_MARA WHERE MTART = 'FERT' .

Step2:Using Select Single in SAP ABAP Normal Top^


Select Single is a statement which is used to read single data from a database table.The below is the
example code for reading single record from MARA table.
Note: Whenever we use select single, we must pass key field in where condition.

**Declare internal table


DATA : WA_MARA TYPE TABLE OF MARA.

*use select * to read data


SELECT SINGLE * FROM MARA INTO WA_MARA WHERE MATNR = '000001'. .
Step3:Using Select Max in SAP ABAP Important Top^
By using this query we can get highest numeric value of a column in SAP Database tables.

Syntax: SELECT MAX( <COLUMNNAME> ) INTO (<VARIABLE>) FROM <TABLE> .

Get Maximum value of a column using Select Max


The below statement will get the maximum value of column LIFNR from LFA1 table.

DATA LV_MAX TYPE LIFNR.


SELECT MAX( LIFNR )
INTO (LV_MAX)
FROM LIFNR .
WRITE:/ LV_MAX.

Step4:Using Select Min in SAP ABAP Important Top^


By using this query we can get minimum value of a column in SAP database table.

Syntax: SELECT MIN( <column> )


INTO (<variable>)
FROM <table>.

Get Minimum Value of a column in SAP ABAP


The below query will get the minimum value of LIFNR (Vendor Account No) from LFA1 table.
DATA LV_MIN TYPE LIFNR.
SELECT MIN( LIFNR )
INTO (LV_MIN)
FROM LFA1 .
WRITE:/ LV_MIN.

Step5:Using Select UP TO in SAP ABAP Normal Top^


BY using Select Up To query we will get the specific no of records from a data base table, it will get
records from starting(begining).

Syntax:select * FROM <TABLE> INTO TABLE <ITAB> UP TO <NUMBEROFROWS> rows.

Get specific number of rows (records) from a database table in SAP ABAP.
data : it_mara type TABLE OF mara.
**Get 50 rows form starting
select * FROM mara INTO TABLE it_mara UP TO 50 rows.

Step6:Using Select Distinct in SAP ABAP Normal Top^


Select Distinct is used to get distinct (unique) values of a particular column in SAP ABAP.
Syntax: SELECT DISTINCT <COULMN> FROM <TABLE> INTO TABLE <ITAB>.

The below example is used to get distinct material type values from MARA table.

REPORT ZSAPN_SELECT_DISTINCT .
TYPES: BEGIN OF TY_MARA,
MTART TYPE MARA-MTART,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.

SELECT DISTINCT MTART FROM MARA INTO TABLE IT_MARA.

LOOP AT IT_MARA INTO WA_MARA.


WRITE:/ WA_MARA-MTART.
ENDLOOP.

Step7:Using Select Order by in SAP ABAP Important Top^


SELECT ORDERBY is used to fetch data from database table with sorted result set, by default the result
will be sorted in ascending order, to sort in descending order you have to specify

Syntax: SELECT * FROM <TABLE> INTO TABLE <ITAB> ORDER BY <COLUMN> ASCENDING/DESCENDING.

The below is the example program of using orderby with select in SAP ABAP.
*example1 Sort in ASCENDING ORDER
REPORT ZSAPN_SORT_ASCENDING .
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA ORDER BY MATNR ASCENDING.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR.ENDLOOP.
*example2 Sort in DESCENDING ORDER
REPORT ZSAPN_SORT_ASCENDING .
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA ORDER BY MATNR DESCENDING.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR.
ENDLOOP.

The above statement is educational purpose only, in your real-time projects don`t use SELECT
ORDERBY, it decreases performance of a program, instead use SORT after fetching data. SORT <ITAB>
ASCENDING/DESCENDING.

Step8:Using Wildcards in Selects Normal Top^


SQL Wildcards are used to search for data in a database table, below are the examples of using
wildcards in SAP ABAP.
The below example will get all records from MARA where MATNR contains 11.

REPORT ZSAPN_WILDCARDS.
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA WHERE matnr LIKE '%11%'.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR.
ENDLOOP.

The below example will get all records from MARA where MATNR ends with 11.

REPORT ZSAPN_WILDCARDS.
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA WHERE matnr LIKE '%11'.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR.
ENDLOOP.

The below example will get all records from MARA where MATNR starts 11.

REPORT ZSAPN_WILDCARDS.
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA WHERE matnr LIKE '11%'.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR.
ENDLOOP.

You might also like