SAP BOX / ABAP

[ABAP SQL for Beginners]

BOXLOGODEVTranslated from Korean 한국어 원문 보기

This English version is a translation of the original Korean post. Text, screenshots and product details reflect the date it was written.

🍇 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

PartMeaningAnalogy
RowOne line of dataOne apple
ColumnAn attribute of the dataThe apple's color, size, taste
TableThe whole containerA 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

TableDescription
SCARRAirline information (Lufthansa, Delta, etc.)
SPFLIBasic flight connection information (departure/arrival cities, etc.)
SFLIGHTFlight 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 codeAirline name
LHLufthansa
SQSingapore Airlines
UAUnited Airlines
WHERE carrid = 'SQ'.  " Practice by switching to Singapore Airlines

📝 Wrap-up

ConceptMeaningExample
TableFridgefridge
RowOne piece of fruitgrape
SELECTTake outSELECT grape
INTOPut inINTO wooden_bowl
Internal tableBowlwooden_bowl, basket

In the end, SQL is a language about "what to take out, and where to put it."

SAP BOXBack to SAP BOX