Register Login

Open SQL syntax examples

Updated May 18, 2018

1. Working with single entries
2. Reading all entries into an internal table
3. Reading singel entries
4. Selecting single fields
5. Append new record
6. Update with where clause
7. For all entries

1. Working with single entries

select * from ska1
where saknr = '77004500'.
* Do something with data
move-corresponding ska1 to itab.
append itab.

2. Reading all entries into an internal table

This is more efficient that example 1

select * from ska1 into table itab
where saknr like '77%'
order by saknr.

3. Reading single entries

You must specify the full primarykey in the where clause to get a correct result.

tables: zsd00004.
data l_custname like like zsd00004-zcustname.

SELECT SINGLE zcustname into l_custname FROM zsd00004
WHERE zcustno = '1551'.

4. Selecting single fields

This could improve effciency for database tables with many entries and many fields.

data: l_kunnr like kna1-kunnr,
l_kukla like kna1-kukla.

SELECT kunnr kukla
INTO (l_kunnr,l_kukla)
FROM kna1.

write: / l_kunnr, l_kukla.

5. Append new record

Tables: zsd00003.

zsd00003-zprogram = g_screen0100-zprogram.
zsd00003-zstep = g_screen0100-zstep.

zsd00003-zenhed = g_screen0100-zenhed.

INSERT INTO zsd00003 VALUES zsd00003.

IF sy-subrc = 4.

SY-SUBRC = 0: Line inserted.
SY-SUBRC = 4: The line could not be inserted. A line with the same key
already existed.

6. Update with where clause

This statement updates the fields zlogdato and zlogtid for the records that
satisfies the where clause.

update zsd00003 set zlogdato = sy-datum
zlogtid = sy-uzeit
where zdriftscenter = g_screen0100-zdriftscenter.

7. For all entries

tables: mara, makt.

data: begin of i_material occurs 0,
matnr like mara-matnr,
maktx like makt-maktx,

end of i_material.


select matnr
appending corresponding fields of table i_material
from mara.

select matnr maktx
into corresponding fields of table i_material
from makt
for all entries in i_material
where matnr = i_material-matnr and
spras = sy-langu.

loop at i_material.
write: / i_material-matnr, i_material-maktx.