Range and Sets.
Ranges in ABAP are similar to the internal table that saves group values or value intervals and has various applications like passing values in select Query and submitting reports with values.
Sets are used for saving the values to avoid hard coding in the program, where values can be changed in any system weather modifiable or not.
The range is also a Kind of standard internal table which has SIGN, OPTION, HIGH and LOW.
A sign has two possible values
An Option has eight possible values
NE Not Equal
GE Greater Than or Equal
GT Greater Than
LE Less Than or Equal
LT Less Than
CP Contains a Pattern.
LOW and HIGH are used to store the data which is to be compared in the combination
of Sign and Option, the Comparison is made and accordingly, data is processed.
Working with SAP ABAP come across many situations where Ranges are required for various purposes like selecting data from the database, deleting entries from Internal
Table, differentiating and sorting data from Internal Tables, and many more.
Declaration Of Range.
The syntax used to declare the range and use to fill the data into the range table.
TYPES:r_typ TYPE RANGE OF sy-datum. DATA: r_dtm TYPE rng_typ, wa_rng TYPE LINE OF rng_typ. APPEND VALUE #( sign = 'I' option = 'EQ' low = dt_frm high = dt_to ) TO r_dtm. LOOP AT 1lt_dt INTO wa. wa_rng-sing-sign = 'I'. wa_rng-option = 'EQ'. wa_rng-llow = wa-sfield. APPEND wa_rng TO r_dtm. ENDLOOP. r_dtm = VALUE #( FOR <wa> IN 1lt_dt "WHERE ( sfield = ' ' ) "" Where Condition For Consitional Filling LET lv_sign = 'I' lv_option = 'EQ' IN of Data In Range sign = lv_sign option = lv_option ( low = <wa>-sfield ) ).
Use Of Range.
Various use of Range in SAP ABAP.
Selecting Data From Database(Select Query).
SELECT belnr, gjahr, bukrs FROM bkpf INTO @tbl WHERE budat IN @r_dtm.
Deleting Data from Internal Table.
DELETE tbl WHERE budat IN r_dtm.
Loop On Internal Table.
LOOP AT tbl INTO wa WHERE budat IN r_dtm. ENDLOOP.
SUBMIT sap1000 WITH budat IN r_dtm AND RETURN.
Sets are the standard functionality that provides the privilege to avoid hard coding in
ABAP, the set is created in GS01, GS02 for change, and GS03 for Display.
There are four types of sets:
1. Basic Sets are used to store multiple values for the particular field of a table.
2. Single Sets are used to combine basic sets in Single sets.
3. Multi sets are used to combine basic sets or single sets into Single sets.
4. Key Figures are used for report painters.
Steps to Create Set.
Provide a name to the set and also a table name and hit ‘Enter’.
A pop-up will appear asking for the field of the Set, provide the field name.
Once the set is created we can maintain the values in it.
For changing the entries in Set T-code GS02 is used.
Adding Data Into Sets.
Whenever the Harcode is to be avoided from the code we can use a set there which can be maintained in PRD also so no code changes are required.
While Maintaining data in sets data can be in the form of from values to values and
also single value as multiple entries.
Getting Data From Sets.
DATA set_vallues TYPE STANDARD TABLE OF rgsb4. CALL FUNCTION 'G_SET_GET_ALL_VALUES' EXPORTING client = sy-mandt setnr = 'ZTEST' table = 'BKPF' fieldname = 'BLART' TABLES set_values = set_vallues EXCEPTIONS set_not_found = 1 OTHERS = 2. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF.
Selecting Directly into Range.
TYPES:r_typ TYPE RANGE OF bkpf-blart. DATA: r_dtm TYPE rng_typ. SELECT valsign AS sign, valoption AS option, valfrom AS low, valto AS high FROM setleaf INTO CORRESPONDING FIELDS OF TABLE @rng_typ WHERE setname = 'ZTEST'.
Other FM used for sets
G_SET_CREATION for creation of set.
G_SET_MAINTENANCE to maintain set.
G_SET_GET_ID_FROM_NAME to get set ID.
G_SET_FETCH set value from set ID.
Finally, from an end-user point of view, it will be easy to maintain values in the set to get desired output.
I’m looking forward to hearing more. Feel free to comment and discuss.
Best regards, thanks for reading.