🍇 ABAP SQL basics: taking fruit out of the fridge (feat. internal tables)
If you learn SAP ABAP for the first time at work or in a government-funded training course, honestly, the hardest part is SQL.
My seniors told me so many times that "if you can write queries well, 80 percent of ABAP is done" that my ears nearly bled.
For fellow non-CS majors like me, I want to share the basic SQL knowledge that finally made sense to me.
Are you new to SAP ABAP,
and does SQL syntax feel difficult and complicated?
Then picture this.
There is fruit in your fridge at home, and taking it out and putting it in a bowl is exactly what SQL does.
✅ What is SQL?
SQL stands for "Structured Query Language."
It is the way you pick only the "fruit" (data) you want out of the "fridge" (table) where data is stored, and put it into a "bowl" (variable).
🍇 Example 1 – Taking out green grapes
SELECT grape
FROM fridge
INTO wooden_bowl
WHERE color = 'green'.
🧠 It is easy if you read it like this:
- fridge → an SAP table full of data
- grape → the information I want to take out
- wooden_bowl → the bowl that holds the data you took out, that is, a variable or internal table
- color = 'green' → pick only the green grapes!
🍽️ So what is the "wooden bowl"? An internal table!
In SAP, when you take data out one row or many rows at a time,
you use a bowl called an internal table.
🍱 What is an internal table?
- It is like a food container.
- It can hold many rows of data at once.
- There are several kinds, and you can define a structure inside it.
🧺 Internal table example – making a basket
TYPES: BEGIN OF ty_fruit,
fruit TYPE char20,
color TYPE char10,
END OF ty_fruit.
DATA: basket TYPE TABLE OF ty_fruit.
It is easy if you read it like this:
- ty_fruit → the blueprint for one piece of fruit
- basket → an internal table that can hold many of these fruits
🍎 Example 2 – Filling the basket with apples
SELECT fruit color
FROM fridge
INTO TABLE basket
WHERE fruit = 'apple'.
- This is SQL that takes out all the apples matching the condition and puts them in the basket at once.
- It is like asking at the grocery store, "Apples, please!" and getting ten of them in a basket.
🛠 Remember the three parts of an internal table
| Part | Meaning | Analogy |
|---|---|---|
| Row | One line of data | One apple |
| Column | An attribute of the data | The apple's color, size, taste |
| Table | The whole container | A basket holding many apples |
🎯 In summary…
- SAP DB table = fridge
- SQL SELECT = the act of taking things out
- Internal table = bowl
- used to hold multiple rows of data,
- and essential when you process or output the data you took out.
📘 Practice tip for beginners – internal tables with the flight tables
🔎 Tables used
| Table | Description |
|---|---|
| SCARR | Airline information (Lufthansa, Delta, etc.) |
| SPFLI | Basic flight connection information (departure/arrival cities, etc.) |
| SFLIGHT | Flight details (seats, price, etc.) |
🧪 Practice goal
Put only one airline's flights into an internal table and print them on screen
Example: read and print only the flights of airline 'LH' (Lufthansa)
✅ Practice code
Paste the code below into SE38 or SE80 and run it.
REPORT zflight_demo.
TYPES: BEGIN OF ty_flight,
carrid TYPE sflight-carrid, "Airline code
connid TYPE sflight-connid, "Flight number
fldate TYPE sflight-fldate, "Flight date
price TYPE sflight-price, "Ticket price
currency TYPE sflight-currency, "Currency
END OF ty_flight.
DATA: it_flights TYPE STANDARD TABLE OF ty_flight WITH EMPTY KEY,
wa_flight TYPE ty_flight.
" Read flight data
SELECT carrid connid fldate price currency
FROM sflight
INTO TABLE it_flights
WHERE carrid = 'LH'. " Lufthansa only
" Print the result
LOOP AT it_flights INTO wa_flight.
WRITE: / 'Airline:', wa_flight-carrid,
'Flight:', wa_flight-connid,
'Date:', wa_flight-fldate,
'Price:', wa_flight-price,
'Currency:', wa_flight-currency.
ENDLOOP.
🔄 Try other airlines too!
| Airline code | Airline name |
|---|---|
| LH | Lufthansa |
| SQ | Singapore Airlines |
| UA | United Airlines |
WHERE carrid = 'SQ'. " Practice by switching to Singapore Airlines
📝 Wrap-up
| Concept | Meaning | Example |
|---|---|---|
| Table | Fridge | fridge |
| Row | One piece of fruit | grape |
| SELECT | Take out | SELECT grape |
| INTO | Put in | INTO wooden_bowl |
| Internal table | Bowl | wooden_bowl, basket |