0% found this document useful (0 votes)
243 views

Dynamic Programming in ABAP - Part 2 - Introduction To Data Reference - SAP Blogs

Uploaded by

Eugene
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
243 views

Dynamic Programming in ABAP - Part 2 - Introduction To Data Reference - SAP Blogs

Uploaded by

Eugene
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

Community

Ask a Question Write a Blog Post Login

Former Member
September 11, 2017 4 minute read

Dynamic Programming in ABAP – Part 2 – Introduction to


Data Reference
Follow RSS feed Like

12 Likes 13,719 Views 13 Comments

Hi,

In my last blog I explained about eld symbols, below is the link for same:

https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-introduction-to- eld-symbols/

In this blog I am going to explain about data references and its signi cance in dynamic programming.

According to SAP documentation, Data references can point to any data objects or to their parts
(components, rows of internal tables, or sections speci ed by o sets and lengths).

So data references are nothing but pointers. It stores the memory address of any data object. But to access
the actual data object which data reference is pointing to, we rst need to deference it using dereferencing
operator ->*.

Di erence between eld symbol and data reference:

Field symbol is a placeholder for data object to which it is assigned and points to the content of data object
hence it can be used at any operand position (no need to dereference it) and works with the content of the
referenced memory area (value semantics).

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 1/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

Data references are pointers to data objects and it contains the memory address of data object (reference
semantics). Data reference cannot be used at operand position directly; it should be dereferenced rst.

Working with data reference:

There can be two types of data references:

Typed Data Reference


Generic Data Reference

Typed Data Reference:

Typed data reference variable can be declared as:

DATA lr_num TYPE REF TO i.


CREATE DATA lr_num.

Here rst statement declares a reference variable lr_num which can point to any data object of type “i”. And
second statement creates an anonymous data object of type “i” and assigns the reference of this data
object to lr_num. Now if we want to change the value of data object, then it can be done by dereferencing
lr_num by using dereference operator ->* as shown below:

DATA lr_num TYPE REF TO i.


CREATE DATA lr_num.

lr_num->* = 2.
WRITE: / lr_num->*.

The output will be 2.

NOTE:

With ABAP 7.40, instead of CREATE DATA, the NEW operator can also be used to create an anonymous
data object and assigns its reference to a data reference variable.

DATA lr_num TYPE REF TO i.


lr_num = NEW #( ).

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 2/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

Assigning existing data object to data reference:

If you want to assign the reference of an existing data object to a data reference, you can use GET
REFERENCE statement.

DATA: lv_num TYPE i VALUE 2.


DATA: lr_num TYPE REF TO i.

GET REFERENCE OF lv_num INTO lr_num.


lr_num->* = 4.
WRITE: / lv_num.

Here lv_num is an existing data object (not anonymous data object). The output would be 4.

NOTE:

With ABAP 7.40, instead of GET REFERENCE, the REF operator also can be used to assign the reference
of an existing data object to a data reference.

Working with structures:

DATA: lr_mara TYPE REF TO mara.

CREATE DATA lr_mara.


lr_mara->matnr = '1111'.
lr_mara->matkl = '03'.

Here individual components of the structure can be accessed with -> operator on data reference variable.

Working with internal tables:

While processing internal table row, we can use REFERENCE INTO statement to set references to table rows
as shown below:

DATA: lr_mara TYPE REF TO mara.


DATA: lt_mara TYPE TABLE OF mara.

SELECT * FROM mara INTO TABLE lt_mara UP TO 10 ROWS.

LOOP AT lt_mara REFERENCE INTO lr_mara.

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 3/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

WRITE: / lr_mara->matnr.
ENDLOOP.

Generic Data Reference:

Generic data reference can be declared as:

DATA: lr_num TYPE REF TO data.


CREATE DATA lr_num TYPE i.

Here rst statement declares a generic data reference lr_num which can point to any data object. And
second statement creates an anonymous data object of type “i” and assigns its reference to lr_num.

‘data’ in ABAP is a generic data type.

Now since lr_num is generic, lr_num->* cannot be directly used at operand position. Hence the below
statement would not be allowed.

lr_num->* = 2.

So in case of generic data reference, it can only be dereferenced using a eld symbol, and this eld symbol
can be used at any operand position to manipulate the value of data object as shown below:

DATA: lr_num TYPE REF TO data.


FIELD-SYMBOLS: <num> TYPE any.

CREATE DATA lr_num TYPE i.


ASSIGN lr_num->* TO <num>.

<num> = 3.

NOTE:

After ASSIGN statement you should check sy-subrc If eld symbol assignment is successful, sy-subrc will
be 0 otherwise it will be 4.

Working with structures:

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 4/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

DATA: lr_str TYPE REF TO data.


FIELD-SYMBOLS: <str> TYPE any.
FIELD-SYMBOLS: <data> TYPE any.
CREATE DATA lr_str TYPE mara.

ASSIGN lr_str->* TO <str>.


ASSIGN COMPONENT 'MATNR' OF STRUCTURE <str> TO <data>.
<data> = '112'.

Here CREATE DATA statement creates an anonymous data object (MARA structure) and assigns its
reference to the generic data reference lr_str, which then can be dereferenced into a generic eld symbol
<str>. Now, to access individual component of MARA structure, ASSIGN COMPONENT statement can be
used.

Dynamically create data objects:

Requirement: Selection screen parameter “Table Name” will take a table name as input and display the
corresponding table entries as output.

Solution:

PARAMETERS: p_tname TYPE tabname.


DATA: lr_tab TYPE REF TO data.
FIELD-SYMBOLS: <tab> TYPE ANY TABLE.

CREATE DATA lr_tab TYPE TABLE OF (p_tname).


ASSIGN lr_tab->* TO <tab>.
IF sy-subrc EQ 0.
SELECT * FROM (p_tname) INTO TABLE <tab> UP TO 10 ROWS.
cl_demo_output=>display( <tab> ).
ENDIF.

Explanation:

Here lr_tab is a generic data reference and <tab> is a generic eld symbol for internal table. In CREATE
DATA statement, the type of data object is mentioned in parenthesis which means that the type will be
determined at runtime based on the value of parameter p_tname. After that we have dereferenced the data
reference lr_tab into a generic eld symbol <tab>. Now this eld symbol can be used to do any valid
operation on the internal table.

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 5/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

Di erence between data reference and object reference:

There are two types of reference variable:

Data reference and


Object reference

Data reference variable can store the reference to any data object (variable, structures, internal tables etc.)
whereas Object reference variable can store the reference to any class object.

For data reference variables, either the generic data type or a completely speci ed data type can be
speci ed. For object reference variables, either a class or an interface can be speci ed.

Thank you for reading.

In next blog, I have put one example of dynamic programming approach:

https://blogs.sap.com/2017/09/29/dynamic-programming-in-abap-part-3-an-example-abap-rtts/

Credits:

https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/abendata_reference_type.htm

Alert Moderator

Assigned tags

ABAP Development | SAP NetWeaver Application Server for ABAP | abap | data | data reference |

View more...

Related Blog Posts

1000+ Pages full of ABAP


By Horst Keller , Mar 20, 2007

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 6/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

Dynamic Programming in ABAP - Part 1 - Introduction to Field Symbols


By Former Member , Sep 05, 2017

Something’s Coming
By Horst Keller , Jan 15, 2007

Related Questions

How to create Dynamic class in abap programming.


By Ajay Rajareddy , Aug 04, 2018

ABAP BOOK
By Former Member , Jun 09, 2005

Dynamic Programming in ABAP


By Former Member , Jun 26, 2012

13 Comments

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

Jelena Per ljeva

September 11, 2017 at 3:39 pm

Can I ask you – what exactly is the point of these blogs?

I have quite a bit of ABAP experience but, to be honest, after reading this I’m none wiser on why would I
need this if I’m not planning to re-invent SE16. If someone has trouble understanding ABAP Help, I doubt
this would be of much help to them either.

Also posting the old syntax and then adding a note of 7.4 syntax could have been justi ed few years ago
when it just became available. But these days I believe it should be the other way around: post current
syntax and make a note of the old one. This is, however, all available in ABAP Help so, again, not sure what
the point of this blog is…

Like (1)

Former Member | Post author

September 11, 2017 at 6:05 pm

Hi Jelena,

I observed that sometimes, ABAP help lacks proper examples, so anyone who is very new in ABAP
development may not be very comfortable in understanding ABAP help. Please correct me if i am wrong.

My point in this blog was to help the readers by showing that dynamic programming in ABAP is achieved
using eld symbols and data reference by putting some examples. Also the table display problem was just

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 7/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

an example to show how to create data object dynamically.

I agree that i should have used 7.4 syntax, will keep this in mind in my future posts, thanks for your guidance

Regards,

Rahul

Like (0)

Jelena Per ljeva

September 12, 2017 at 5:39 pm

This may have been true many years ago but currently ABAP keyword documentation is available online and
it has very good examples and thorough explanations, from what I see. Unfortunately, I can’t nd how to get
a link to a speci c article but this is a general link for documentation and a sample screenshot:

If someone is using ABAP Help in their outdated SAP system I’d encourage them to use online
documentation to supplement it.

Like (0)

Michelle Crapo

September 19, 2017 at 5:59 pm

Another nice t-code for help is ABAPDOCU.   It comes complete with documented program examples.

 
https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 8/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

Like (1)

John Voorhees

July 23, 2019 at 10:15 pm

Jelena, So I strongly disagree with you!! I am a new ABAPer. After working with several other languages C++,
Java, PHP and BPMS.

While I can read the documentation which can be verbose , I found this blog and his one on Field Symbols
most useful to get up to speed quickly

Keep it up!   

Like (1)

Jay Desai

November 1, 2019 at 7:43 am

@Jelena – Humbly disagree.

SAP help language and examples can be tougher to understand for beginners. This is much more simpler
and i bene ted from it.

Thanks Rahul for the blog.

Like (0)

Jelena Per ljeva

November 1, 2019 at 4:06 pm

It’s OK to disagree, di erent people – di erent opinions.

Yes, there are some ABAP Help (not to be confused with SAP Help) articles that are di cult to understand.
But in this particular case personally I nd ABAP documentation and examples quite adequate and
language in this blog doesn’t seem any simpler to understand to me (maybe I’m stupid, dunno ). And I’m
scratching my head on what exactly causes di culties for the beginners in this case. So far the comments
just state disagreement but it’d be nice to o er some speci c examples, like what exactly is not clear in
ABAP Help. (CC Horst Keller )

Like (0)

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 9/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

Jacques Nomssi

September 12, 2017 at 8:37 am

Hello Rahul,

you can pass references to routines/methods as IMPORTING parameters, and still be able to change the
referenced object. In this use case the IS BOUND predicate expression is useful to check if a data reference
is valid.

best regards,

JNN

Like (1)

Former Member | Post author

September 12, 2017 at 8:48 am

Correct Jacques, I forgot to put this point.

We should always check if the data reference is valid using IS BOUND predicate, just like we use IS
ASSIGNED for validating eld symbol.

Thanks.

Like (0)

Michelle Crapo

September 19, 2017 at 6:30 pm

Hi Rahul,

Again this is a nice start.   I know that a lot of us are still on the older systems. (SAP GUI)  So I tend to look
for these “older” blogs.   With that thought – it would help if this read less like a index from a book or a help
doc.   Perhaps a nice program to illustrate your points would be helpful instead of the small snippets.    Show
that one program that “encapsulates” what you are trying to do.   I know you’ve written a dynamic program.
  Show it!  Then maybe in pieces explain it.

Think more about what the business problem/demand caused you to write a dynamic program.   Why?
 Easier to understand?  Quicker to code?   Multiple uses?

This really reads more like a help document.

Just a thought!

Michelle

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 10/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

Do Keep blogging!   Very few are brave enough to use the “older” technology and blog about it.

Like (2)

Former Member | Post author

September 20, 2017 at 6:48 am

Thanks Michelle for your suggestions, I will try to put a good example demonstrating dynamic programming
in next blog.

Regards,

Rahul

Like (0)

Angel Sabogal

September 4, 2019 at 10:21 pm

It’s the best explanation I’ve ever seen. To be honest I prefer this snippets that seeking in a program the way
to understand the topic. Thanks for posting.

Like (0)

Sandra Rossi

September 5, 2019 at 7:25 am

Thank you for blogging about this concept often not well understood, and to summary the important
notions all in one.

Just to nitpicking:

It’s always good to propose the link to the “texts referenced”, so that to help readers learn more about the
topic -> ABAP docu 7.53 – Data References (“Data references can point to any data objects or to their
parts (components, rows of internal tables, or sections speci ed by o sets and lengths).“)
Typo: deference -> dereference
“Data reference cannot be used at operand position directly”: not entirely true if taken out of the context
of your post, because you may pass a data reference as a parameter without dereferencing it (more
frequent with object references/okay I admit you’re only talking about data references), and the
procedure will dereference it later.
other possible syntax: dref = NEW i( 555 ).
“After ASSIGN statement you should check sy-subrc“: no, it is set only for the “dynamic variants” of
ASSIGN. ASSIGN dref->*, ASSIGN dobj, etc., don’t set SY-SUBRC. But ASSIGN (var), ASSIGN
COMPONENT, etc., do set SY-SUBRC.
https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 11/12
2/13/2020 Dynamic Programming in ABAP – Part 2 – Introduction to Data Reference | SAP Blogs

ABAP docu 7.53 – ASSIGN


ABAP docu 7.53 – CREATE DATA
ABAP docu 7.53 – NEW
ABAP docu 7.53 – REF

Like (3)

Find us on

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Настройки параметра cookie

Newsletter Support

https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-introduction-to-data-reference/ 12/12

You might also like