Professional Documents
Culture Documents
Sap Abap SQL Select Statements With Examaple
Sap Abap SQL Select Statements With Examaple
The below is the example code for reading data from MARA table.
The below example code is used to get data based on a condition with all fields(columns) from MARA
table.
* read data
The below is the example code for reading single record from MARA table.
Note: When ever we use select single, we must pass key field in where condition.
The below statement will get the maximum value of column LIFNR from LFA1 table.
INTO (LV_MAX)
FROM LIFNR .
WRITE:/ LV_MAX.
INTO (<variable>)
FROM <table>.
The below query will get the minimum value of LIFNR (Vendor Account No) from LFA1 table.
INTO (LV_MIN)
FROM LFA1 .
WRITE:/ LV_MIN.
Step5:Using Select UP TO in SAP ABAP Normal
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).
Get specific number of rows (records) from a database table in SAP ABAP.
The below example is used to get distinct material type values from MARA table.
REPORT ZSAPN_SELECT_DISTINCT .
END OF TY_MARA.
WRITE:/ WA_MARA-MTART.
ENDLOOP.
Step7:Using Select Order by in SAP ABAP Important
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.
The below is the example program of using orderby with select in SAP ABAP.
REPORT ZSAPN_SORT_ASCENDING .
WRITE:/ WA_MARA-MATNR.ENDLOOP.
REPORT ZSAPN_SORT_ASCENDING .
WRITE:/ WA_MARA-MATNR.
ENDLOOP.
Step8:Using Wildcards in Selects Normal
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.
SELECT * FROM MARA INTO TABLE IT_MARA WHERE matnr LIKE '%11%'.
WRITE:/ WA_MARA-MATNR.
ENDLOOP.
The below example will get all records from MARA where MATNR ends with 11.
REPORT ZSAPN_WILDCARDS.
SELECT * FROM MARA INTO TABLE IT_MARA WHERE matnr LIKE '%11'.
WRITE:/ WA_MARA-MATNR.
ENDLOOP.
The below example will get all records from MARA where MATNR starts 11.
REPORT ZSAPN_WILDCARDS.
SELECT * FROM MARA INTO TABLE IT_MARA WHERE matnr LIKE '11%'.