Technical Articles
Easy ways to create and populate range tables in ABAP 7.4 onwards
Introduction
In certain cases, you need to create and populate range tables in ABAP for different purposes. So, in this blog post, I have explained 3 easy ways to populate range tables.
Solution
The ways are the following.
- Using LET with VALUE. Please check the below section for quick reference.
TYPES lr_bukrs_type TYPE RANGE OF bukrs. DATA : lr_bukrs_01 TYPE lr_bukrs_type, "Table 1 lr_bukrs_02 TYPE lr_bukrs_type. "Table 2 * Using only LET with VALUE *--------------------------------------------------------------------* lr_bukrs_01 = VALUE lr_bukrs_type( LET s = 'I' o = 'EQ' IN sign = s option = o ( low = '0100' ) ( low = '0200' ) ( low = '0300' ) ( low = '0400' ) ( low = '0500' ) ( low = '0600' ) ( low = '0700' ) ).
- Using MACRO: This is the simplest way. Please check the below section for quick reference.
TYPES lr_bukrs_type TYPE RANGE OF bukrs. DATA : lr_bukrs_01 TYPE lr_bukrs_type, "Table 1 lr_bukrs_02 TYPE lr_bukrs_type. "Table 2 * Using MACRO *--------------------------------------------------------------------* DEFINE range_bukrs. lr_bukrs_01 = VALUE lr_bukrs_type( BASE lr_bukrs_01 ( sign = 'I' option = 'EQ' low = &1 ) ). END-OF-DEFINITION. *--------------------------------------------------------------------* range_bukrs: '0100', '0200', '0300', '0400', '0500', '0600', '0700'. *--------------------------------------------------------------------*
- Using FOR LOOP: By this procedure, one can populate the range table w.r.t another table. Please check the below section for quick reference.
TYPES lr_bukrs_type TYPE RANGE OF bukrs. DATA : lr_bukrs_01 TYPE lr_bukrs_type, "Table 1 lr_bukrs_02 TYPE lr_bukrs_type. "Table 2 * Using MACRO DEFINE range_bukrs. lr_bukrs_01 = VALUE lr_bukrs_type( BASE lr_bukrs_01 ( sign = 'I' option = 'EQ' low = &1 ) ). END-OF-DEFINITION. range_bukrs: '0100', '0200', '0300', '0400', '0500', '0600', '0700'. * Combining LET with FOR LOOP *--------------------------------------------------------------------* lr_bukrs_02 = VALUE #( FOR ls_bukrs IN lr_bukrs_01 * ( sign = 'I' "Alternatively * option = 'EQ' "Alternatively LET s = 'I' o = 'EQ' IN sign = s option = o ( low = ls_bukrs-low ) ).
That’s it. 🙂
the first example can still be simplified,
I personally find the other options not elegant and rather complicated.
Dear Andre,
Thanks for reviewing the blog post. 🙂
Yes, the example that you have provided is also right. The 'LET' keyword creates complicacy sometimes.
Regards,
Kallol
I actually like all three of the examples. It drives me crazy to create those range tables. My personal preference is the macro. However, someplace around here I read "Macros were bad". Not sure where...
My old way of doing it - is a simple loop within a class where the table and the field are dynamically passed. I know not very elegant - Hence I'm not sharing. 🙂 I like these better.
You're right. There're two main reasons that I feel macros are not so good:
Here's where you can also read it: ABAP Programming Guidelines on Macros 🙂
It basically says "Only use macros in exceptional cases", but also that simple cases (like I would say this one is) are just fine...
Personally, as one of those "decisions that make other decisions easier", I prefer not to use them at all. For ranges specifically I use the Andre Kuller version without the additional LET...
There's nothing you can do with a macro that you can't do equally well using methods or VALUE or ASSIGN.
If you feel there is somewhere where you need it, I'd bet you've got a bad program design! 😀
TL;DR Don't use macros. They're BAAAAAAAAAAAAAAAAAAAAAAD.
My 2 cents...
Perhaps:
But i remember having some problem declaring the ranges in-line. Therefore, I do not use this construct.
High_Low_Diff_Datatype
I prefer declaring the range explicitly & then using it in the select.
Good point. Maybe
would work.
Better yet, use cast
Don't use macros - reason given above.
I use:
I seem to recall you can also use
Well dang - I like these even better. My loop/end loop might be around a T table. So I'd have to see how it works - one line at a time.
I can see I need to think about ranges a bit longer.
As far as ranges go, I'd like to see
... TYPE LINE OF RANGE OF ...
Don't you mean this? (like Andre Kuller answer)
I may well mean that...
I love this blog. Just for all the comments it's been generating. It proves to me - again - it's worth writing a blog sometimes just to read the comments.
Sometimes you need to create a range table from another internal table. I use an SQL Expression for that. In the example below docitems is an internal table containing the BUKRS field.
Instead of blank spaces, can specify exact number
Can I ask you please on what release you are? This does not work on ABAP 7.50. Perhaps you are beyond this version?
This is really a helpful blog I am really impressed with your work, keep it up the good work
We can also convert any column in internal table to a Rangle table in a single line of code like below.
DATA(RT_PERNR) = VALUE RSDSSELOPT_T( FOR WA_EMP1 IN IT_EMP ( SIGN = IF_FSBP_CONST_RANGE=>SIGN_INCLUDE OPTION = IF_FSBP_CONST_RANGE=>OPTION_EQUAL LOW = WA_EMP1-PERNR ) ).