Tuesday, 12 September 2017

Dynamic Programming in ABAP: Part 2 - Introduction to Data Reference

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

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

In this blog I am going to explain about data references and its significance 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 specified by offsets and lengths).

SAP ABAP, SAP ABAP Certifications, SAP ABAP Live, ABAP Learning, SAP ABAP Tutorials and Materials

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 first need to deference it using dereferencing operator ->*.

Difference between field 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).

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

Working with data reference:

There can be two types of data references:

◉ Typed Data Reference
◉ Generic Data Reference


1. Typed Data Reference:


Typed data reference variable can be declared as:

DATA lr_num TYPE REF TO i.
CREATE DATA lr_num.

Here first 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 #( ).

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.
  WRITE: / lr_mara->matnr.
ENDLOOP.

2. Generic Data Reference:


Generic data reference can be declared as:

DATA: lr_num TYPE REF TO data.
CREATE DATA lr_num TYPE i.

Here first 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 field symbol, and this field 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 field symbol assignment is successful, sy-subrc will be 0 otherwise it will be 4.
----------------------------------------------------------------------------------------------------------

Working with structures:

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 field 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 field 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 field symbol <tab>. Now this field symbol can be used to do any valid operation on the internal table.

Difference 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 specified data type can be specified. For object reference variables, either a class or an interface can be specified.

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

Follow us on Twitter: https://twitter.com/saponlineguides
Google+: https://plus.google.com/b/107454975947044053086/107454975947044053086
Facebook:  https://www.facebook.com/saponlineguides/
SAP Online Guides, Tutorials, Materials and Certifications.

Related Posts

71 comments:

  1. Data Science Online (Live Instructor-Led Virtual Classroom Training):
    ExcelR offers instructor-led live online Data Science training for the individuals who cannot attend the classroom training. We conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning
    Participants can see and interact with the trainer, have meaningful discussions without missing the flavour of classroom training at their own pace and convenience.
    ExcelR Data Analytics Courses

    ReplyDelete
  2. ExcelR offers both classroom and instructor-led live online Data Analytics training. We also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning.
    Participants can see and interact with the trainer, have meaningful discussions without missing the flavour of classroom training at their own pace and convenience.
    ExcelR Data Analytics Course Data Analytics Course Mumbai

    ReplyDelete
  3. This post is extremely radiant. I extremely like this post. It is outstanding amongst other posts that I’ve read in quite a while. Much obliged for this better than average post. I truly value it! sap training institute in hyderabad

    ReplyDelete
  4. The first and foremost thing when learning data science is the discovery of data insight. In this aspect, the raw data is analyzed to gather information from raw data.

    data science course in gorakhpur

    ReplyDelete
  5. The blog provides a clear and comprehensive explanation of data references and their role in dynamic programming in ABAP. It effectively distinguishes between field symbols and data references, highlighting the importance of dereferencing when working with data references. The examples on typed and generic data references, especially when dealing with structures and internal tables, make the concepts easy to grasp. The inclusion of practical scenarios, such as dynamically creating data objects, adds real-world value to the content. The comparison between data references and object references is also insightful, offering clarity on their respective uses. Overall, this blog is an excellent continuation of the dynamic programming series, providing both depth and practicality.
    data analytics courses in dubai

    ReplyDelete
  6. Very insightful post! The landscape of data science is constantly evolving. For anyone eager to learn, I recommend the Data science courses in Faridabad. They provide hands-on experience that can make a difference.

    ReplyDelete
  7. This post was enlightening! Thank you for your thoughtful approach. Loved this! Your writing is both informative and enjoyable
    Data science courses in Gujarat

    ReplyDelete
  8. Great job on "Dynamic Programming in ABAP: Part 2 - Introduction to Data Reference"! Your detailed explanations make complex concepts more accessible. Keep sharing your expertise; it's inspiring and incredibly helpful for developers looking to deepen their understanding of ABAP. Keep up the fantastic work!
    Data Science Courses in Singapore

    ReplyDelete
  9. This is a fantastic overview of dynamic programming in ABAP! It's exciting to see how dynamic techniques can enhance flexibility and efficiency in our applications. The examples you provided really illustrate the power of dynamic data handling and how it can simplify complex logic. I’m eager to implement some of these strategies in my own projects. Thanks for sharing such valuable insights—definitely a must-read for ABAP developers looking to level up their skills!
    Online Data Science Course

    ReplyDelete
  10. Thanks for this detailed introduction to data references in ABAP! Your explanations of dynamic programming concepts are very clear and insightful.
    Data science courses in Bhutan

    ReplyDelete
  11. This was such a well-written and informative post on Dynamic programmimg in ABAP ! I really appreciate how you broke down a complex topic in easy way. Your examples made the concepts much clearer. Keep up the great work.
    Online Data Science Course







    ReplyDelete
  12. Very well written article and very well explained. Nice work done.
    Data Science Courses in Hauz Khas

    ReplyDelete
  13. "This post about the Data Science Course in Dadar is super informative!
    The detailed syllabus is impressive and covers a wide range of skills.
    I appreciate the focus on practical applications of data science.
    Such local opportunities are fantastic for career growth.
    I’m eager to learn more about how to enroll!"

    ReplyDelete
  14. Great post! The distinctions between field symbols and data references are well explained, especially in terms of value vs. reference semantics. Data science courses in Mysore

    ReplyDelete
  15. I have gone through your article. Dynamic Programming in ABAP is a powerful tool that allows developers to write more flexible and adaptable code, particularly. By leveraging dynamic programming, ABAP developers can significantly reduce the need for hard-coded logic, making their applications more modular and easier to maintain.
    Thank you for the great content.

    Data science Courses in Germany


    ReplyDelete
  16. "I couldn’t agree more! The future is all about data, and having access to courses locally is a game changer. If you're in Iraq and interested in getting into data science, visit Data science courses in Iraq to find the best courses available."

    ReplyDelete
  17. This is a great follow-up post on dynamic programming in ABAP! You’ve done an excellent job introducing the concept of data references and explaining how they can be used to manage complex data structures. The examples provided make it easier to understand the practical applications of this technique. Thanks for sharing this helpful guide.
    Data science course in Gurgaon

    ReplyDelete
  18. This is such a well-written and informative post. Your tips on Dynamic Programming in ABAP- Introduction to Data Reference are really actionable. Thanks for the step by step guidance.
    Data Science Courses in China


    ReplyDelete
  19. "A great continuation of your dynamic programming series! This part on data references in ABAP is really insightful. It helps clarify the concept and gives a deeper understanding of how to manipulate data more effectively in ABAP. I’m looking forward to more in this series!
    Data science courses in pune

    ReplyDelete
  20. This is a fantastic overview of dynamic programming in ABAP. The examples you provided really illustrate the power of dynamic data handling and how it can simplify complex logic. Thanks for sharing such valuable insights.
    Data Science Courses in Micronesia

    https://iimskills.com/data-science-courses-in-micronesia/

    Data Science Courses in Micronesia

    ReplyDelete
  21. Informative guide on dynamic programming in ABAP! The explanation of dynamic data creation, field symbols, and runtime objects is detailed and easy to follow. Including practical examples demonstrates how to implement flexibility and efficiency in coding. A valuable resource for developers looking to enhance their ABAP skills with dynamic programming techniques!
    business analyst course in bangalore

    ReplyDelete
  22. This post is extremely radiant. I extremely like this post. Data science courses in France

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. This blog provides a detailed explanation of data references in ABAP, including their types, usage, and differences from field symbols. It also covers working with structures and internal tables, and dynamically creating data objects. The clear examples and practical notes make it a valuable resource for ABAP developers.
    Medical Coding Course

    ReplyDelete
  25. Thank you for this well-written and informative post! I really enjoyed reading it. If you'd like to explore more, take a look at OneUp Networks.

    ReplyDelete
  26. This blog offers a clear and concise explanation of Data References in ABAP, highlighting key concepts like typed vs generic references and their use cases with structures and internal tables. The examples provided help to illustrate how to dynamically work with data objects, making it a useful guide for ABAP developers looking to enhance their dynamic programming skills. Investment Banking Course


    Medical Coding Course in Hyderabad

    ReplyDelete
  27. Amazing post does a fantastic job of demystifying the concept of data references in ABAP, presenting the topic with clarity and practical examples. It’s an excellent guide for anyone diving into dynamic programming with ABAP.

    https://iimskills.com/medical-coding-courses-in-delhi/

    ReplyDelete
  28. IIM SKILLS’ Digital Marketing program was incredibly in-depth and gave me a comprehensive understanding of marketing strategies
    Medical Coding Courses in Coimbatore

    ReplyDelete
  29. Great continuation of your series on dynamic programming in ABAP! Keep sharing these valuable insights!
    Medical coding courses in Delhi/

    ReplyDelete
  30. I always find valuable takeaways from your posts. Keep up the great work!
    Medical Coding Courses in Chennai

    ReplyDelete
  31. This comment has been removed by the author.

    ReplyDelete
  32. Your posts are always full of fresh ideas. I can’t wait to see what you write about next!" Medical Coding Courses in Delhi

    ReplyDelete
  33. Really helpful content, thanks for sharing
    https://iimskills.com/medical-coding-courses-in-hyderabad/

    ReplyDelete
  34. Amazing post does a fantastic job of demystifying the concept of data references in ABAP, presenting the topic with clarity and practical examples. It’s an excellent guide for anyone diving into dynamic programming with ABAP.
    https://iimskills.com/medical-coding-courses-in-bangalore/

    ReplyDelete
  35. Thank for diving deep into Dynamic Programming in ABAP. https://iimskills.com/data-science-courses-in-india/

    ReplyDelete
  36. Thank for diving deep into Dynamic Programming in ABAP.
    Data Science Courses in India

    ReplyDelete
  37. Very detailed blog on Data Reference . Thanks for the share.
    technical writing course

    ReplyDelete
  38. Excellent explanation of data references in ABAP! I really appreciate how you've broken down the differences between field symbols and data references, as well as the practical examples of working with typed and generic data references. Your examples make it easier to understand dynamic programming in ABAP. Well done!

    Data Science Courses in India

    ReplyDelete
  39. This is a clear and concise introduction to Contract First Development in WCF 4.5! It effectively highlights the key advantage of avoiding .NET-specific data types in the contract by starting with a WSDL. The step-by-step guide with the XML schema and the generated C# code provides a practical understanding of the process.

    Data Science Courses in India

    ReplyDelete
  40. I can see how much effort went into creating this content—it’s professional, clear, and useful. Medical Coding Courses in Vadodara

    ReplyDelete
  41. Dynamic Programming in ABAP Part 2 introduces data references, enabling flexible and efficient data handling. By using data references, developers can dynamically access and manipulate data objects at runtime. This technique enhances ABAP programs’ adaptability, allowing complex data structures and operations without static declarations.
    Data Science Courses in India

    ReplyDelete
  42. The explanation of data references as pointers to data objects, and the distinction between field symbols and data references, was very clear. I appreciate the examples demonstrating how to work with typed and generic data references, including creating anonymous data objects and assigning existing ones.
    Medical Coding Courses in Delhi

    ReplyDelete
  43. This blog provides a clear and concise explanation of data references in ABAP, highlighting their significance in dynamic programming. The examples effectively demonstrate how to create and manipulate data references, making complex concepts more accessible. Thank you for sharing this valuable resource!
    Medical Coding Courses in Delhi

    ReplyDelete
  44. Nice explanation about data references in ABAP! This makes understanding dynamic programming concepts much easier.
    Medical Coding Courses in Delhi

    ReplyDelete
  45. Excellent continuation to your dynamic programming series! This deep dive into data references really helps clarify the practical differences between field symbols and data references—especially the emphasis on value vs. reference semantics. Medical Coding Courses in Kochi

    ReplyDelete
  46. Great introduction to ABAP data references—clear and informative!

    Medical Coding Courses in Kochi

    ReplyDelete
  47. Thank you for sharing this insightful guide on contract-first development in WCF 4.5. Your detailed explanation enhances understanding of this approach.

    Medical Coding Courses in Kochi

    ReplyDelete
  48. This content was very relatable and timely—keep publishing more like this.
    Medical Coding Courses in Delhi

    ReplyDelete
  49. Great explanation. It really helped me understand better.
    Medical Coding Courses in Delhi

    ReplyDelete
  50. Thanks for this clear and comprehensive explanation on Data References in ABAP! The comparison between field symbols and data references, as well as the examples with structures and internal tables, makes dynamic programming in ABAP much easier to grasp.

    Medical Coding Courses in Delhi

    ReplyDelete
  51. Thanks for the clear explanation of data references in ABAP! The distinction between field symbols and data references really helps clarify their different use cases.
    Medical Coding Courses in Delhi

    ReplyDelete
  52. Very insightful post! Dynamic programming in ABAP can be quite powerful, and understanding data references is key to writing flexible, modular code.
    Medical Coding Courses in Delhi

    ReplyDelete
  53. Excellent continuation! Exploring data references in ABAP dynamic programming adds powerful flexibility to your coding toolkit. Clear and well-explained.
    Medical Coding Courses in Delhi

    ReplyDelete
  54. This explanation of data references in ABAP was incredibly helpful! The comparison between field symbols and data references really clarified when to use each, especially in dynamic scenarios. I also liked how you showed both the traditional and NEW syntax for creating references—very practical examples.
    Medical Coding Courses in Delhi

    ReplyDelete
  55. Great explanation on data references in ABAP! This blog is especially helpful in understanding the difference between field symbols and data references in dynamic programming. Also, if you're exploring healthcare IT, check out these Medical Coding Courses in Delhi.

    ReplyDelete
  56. Here's a concise and meaningful comment you could leave on that blog post:

    ---

    Thanks for the great explanation of dynamic ABAP programming! Your walk-through on using data references, field symbols, and RTTS (Run Time Type Services) to build dynamic structures is really helpful. It provides clear insights into how to work with flexible data types at runtime. Looking forward to more practical ABAP content like this!
    Medical Coding Courses in Delhi

    ReplyDelete
  57. Very informative and well-explained! Data references in ABAP can be tricky to grasp, but your examples and clear explanations make the concept much more accessible. This post is a great resource for developers working on dynamic programming. Thanks for sharing such valuable content!


    Medical Coding Courses in Delhi

    ReplyDelete
  58. This post provides a clear introduction to data references in ABAP, explaining how they act as pointers to dynamically created or existing data objects. It highlights the difference between data references and field symbols and shows how these techniques enable flexible, runtime-adaptable programming. A helpful guide for anyone exploring dynamic programming in ABAP. Do check out Medical Coding Courses in Delhi for more career opportunities.

    ReplyDelete