Easy ways to create and populate range tables in ABAP 7.4 onwards

作者:郑德鼎 约 5 分钟阅读 更新日期:2024-02-22 2 年前更新 标签:ABAP, 开发
目录

Kallol Chakraborty

May 19, 2021 1 minute read

Easy ways to create and populate range tables in ABAP 7.4 onwards

2233102,570

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.

FollowLikeRSS Feed

22 Comments

You must be Logged on to comment or reply to a post.

Andre Kuller

May 19, 2021 at 11:35 am

the first example can still be simplified,

DATA(burks_range) = VALUE lr_bukrs_type( sign = 'I' option = 'EQ'

( low = '0100' )

( low = '0200' )

( low = '0300' )

( low = '0400' )

( low = '0500' )

( low = '0600' )

( low = '0700' ) ).

I personally find the other options not elegant and rather complicated.

Like 13

Share

Kallol Chakraborty

Blog Post Author

May 19, 2021 at 11:54 am

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

Like 3

Share

Michelle Crapo

May 19, 2021 at 3:42 pm

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.

Like 3

Share

Mithun Kumar

May 20, 2021 at 2:04 am

You're right. There're two main reasons that I feel macros are not so good

You cannot debug through a macro. That's a biggest deal-breaker for me, as it's bad maintainability.

In this sort of example, it might still be considered if the 'filling' in range table is being done many times, but for one time it's an overkill. Moreover, even for many times, I wouldn't mind writing the 1 statement a few times as Andre shows in his comment above.

Like 3

Share

Jörgen Lindqvist

May 20, 2021 at 4:02 am

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 UserName Placeholder for UXP-4318 UserName Placeholder for UXP-4318 version without the additional LET...

Like 3

Share

Matthew Billingham

May 20, 2021 at 9:11 am

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.

Like 5

Share

Suhas Saha

May 20, 2021 at 9:01 am

My 2 cents...

data RANGE_OF_COMP_CODES type range of BUKRS.

select 'I', 'EQ', BUKRS from T001 up to 10 rows

into table @RANGE_OF_COMP_CODES.

Like 3

Share

Matthew Billingham

May 20, 2021 at 9:12 am

Perhaps

SELECT 'I' AS sign, 'EQ' AS option, bukrs AS low, @space AS high

FROM t001 UP TO 10 ROWS

INTO TABLE @DATA(range_of_comp_codes).

Like 5

Share

Suhas Saha

May 20, 2021 at 10:20 am

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.

Like 3

Share

Matthew Billingham

May 20, 2021 at 10:41 am

Good point. Maybe

` ` AS high

would work.

Like 1

Share

Aditya Waghmare

January 31, 2022 at 5:58 am

Better yet, use cast

SELECT 'I' AS SIGN, 'EQ' AS OPTION, BUKRS AS LOW, CAST( @SPACE AS CHAR( 4 ) ) AS HIGH

FROM T001 UP TO 10 ROWS

INTO TABLE @DATA(RANGE_OF_COMP_CODES).

Like 0

Share

Matthew Billingham

May 20, 2021 at 9:17 am

Don't use macros - reason given above.

I use

DATA range_of_strings TYPE RANGE OF strings.

range_of_strings = VALUE #( ( sign = 'I' option = 'EQ' low = `cotton`)

( sign = 'I' option = 'EQ' low = `rope` )

( sign = 'I' option = 'EQ' low = `hemp` ) ).

I seem to recall you can also use

DATA range_of_strings TYPE RANGE OF strings.

range_of_strings = VALUE #( ( sign = 'I' option = 'EQ' low = `cotton`)

( low = `rope` )

( low = `hemp` ) ).

Like 4

Share

Michelle Crapo

May 20, 2021 at 12:13 pm

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.

Like 0

Share

Matthew Billingham

May 20, 2021 at 12:24 pm

As far as ranges go, I'd like to see

... TYPE LINE OF RANGE OF ...

Like 0

Share

Sandra Rossi

May 20, 2021 at 12:50 pm

Don't you mean this? (like Andre Kuller answer)

DATA range_of_strings TYPE RANGE OF strings.

range_of_strings = VALUE #( sign = 'I' option = 'EQ' ( low = `cotton`)

( low = `rope` )

( low = `hemp` ) ).

Like 3

Share

Matthew Billingham

May 20, 2021 at 4:33 pm

I may well mean that...

Like 1

Share

Michelle Crapo

May 20, 2021 at 12:15 pm

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.

Like 8

Share

B. Meijs

May 20, 2021 at 2:06 pm

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.

SELECT DISTINCT

'I' AS sign,

'EQ' AS option,

bukrs AS low,

CAST( ' ' AS CHAR ) AS high

FROM @docitems AS docitems

INTO TABLE @DATA(bukrs_range).

Like 1

Share

Aditya Waghmare

January 31, 2022 at 6:03 am

Instead of blank spaces, can specify exact number

SELECT 'I' AS SIGN, 'EQ' AS OPTION, BUKRS AS LOW, CAST( @SPACE AS CHAR( 4 ) ) AS HIGH

FROM T001 UP TO 10 ROWS

INTO TABLE @DATA(RANGE_OF_COMP_CODES).

Like 0

Share

Alberto RADICATI

January 31, 2023 at 9:33 am

Can I ask you please on what release you are? This does not work on ABAP 7.50. Perhaps you are beyond this version?

Like 0

Share

Alira Faleiro

May 24, 2021 at 8:42 am

This is really a helpful blog I am really impressed with your work, keep it up the good work

Like 1

Share

Namasivayam Naveen G

October 14, 2022 at 6:11 am

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 ) ).

郑德鼎

关于作者:郑德鼎

企业信息化与 SAP 技术顾问,长期专注 SAP ABAP、FI/CO、MM、SD 等模块的技术分享与实战经验总结。查看更多介绍

来源说明:本文内容由「Easy ways to create and populate range tables in ABAP 7.4 onwards.docx」整理生成,仅用于内部技术分享与学习交流。