ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Operands and Expressions → ABAP SQL - SQL Expressions sql_exp → sql_exp - sql_arith
SQL expressions, arithmetic calculations
This example demonstrates arithmetic calculations in SQL expressions.
Other versions:
7.31 | 7.40 | 7.54
Source Code
DATA(rnd) = cl_abap_random_int=>create(
seed = CONV i( sy-uzeit ) min = 1 max = 100 ).
DELETE FROM demo_expressions.
INSERT demo_expressions FROM TABLE @( VALUE #(
FOR i = 0 UNTIL i > 9
( id = |{ i }|
num1 = rnd->get_next( )
num2 = rnd->get_next( ) ) ) ).
DATA(offset) = 10000.
SELECT id, num1, num2,
CAST( num1 AS D34N ) / CAST( num2 AS D34N ) AS ratio,
DIV( num1, num2 ) AS div,
DIVISION( num1, num2, 2 ) AS division,
MOD( num1, num2 ) AS mod,
@offset + ABS( num1 - num2 ) AS sum
FROM demo_expressions
ORDER BY sum DESCENDING
INTO TABLE @DATA(results).
cl_demo_output=>display( results ).
Description
Calculations are made and functions called in a list of columns specified after SELECT.
- To perform floating point division with integer columns,
these columns are updated to the type DECFLOAT34 using
cast.
- A host variable is added to the absolute value of a difference between column values.
Using the alias names defined after AS, the results are assigned to the columns
with the same names of an internal table declared inline, results. The result of the final calculation are used as a sort criterion.