{"id":1357,"date":"2015-03-23T15:49:22","date_gmt":"2015-03-23T14:49:22","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1357"},"modified":"2015-03-23T15:51:34","modified_gmt":"2015-03-23T14:51:34","slug":"abap-working-with-dynamicallly-accessed-data","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1357","title":{"rendered":"ABAP &#8211; Working with dynamicallly accessed data"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\" size-full wp-image-358 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" alt=\"SAP\" width=\"44\" height=\"50\" \/>In this article I&#8217;d like to present different possible approaches on accessing data and working with it dynamically.<!--more--><\/p>\n<h2>Your table name is initially unknown and is determined during runtime:<\/h2>\n<pre lang=\"abap\">DATA:\r\n* This will be global variable holding the selected \r\n* table data\r\n  lo_data      TYPE REF TO data,\r\n  lv_tabname   TYPE tabname16 DEFAULT 'MARA',\r\n  lv_component TYPE fieldname DEFAULT 'MSTAE',\r\n  lv_value     TYPE string.\r\n\r\nFIELD-SYMBOLS:\r\n* This will be used to access the table data\r\n* dynamically:\r\n  &lt;lt_data&gt;     TYPE ANY TABLE,    \" Table data\r\n  &lt;ls_data_line&gt;  TYPE ANY,        \" Table line structure  \r\n  &lt;lv_data_field&gt; TYPE ANY.        \" Field of the structure \r\n\r\nSTART-OF-SELECTION.\r\n* Create variable of given type dynamically \r\n  CREATE DATA lo_data TYPE TABLE OF (lv_tabname).\r\n\r\n* Assign contents of the variable to field symbol\r\n* so you can manipulate the data\r\n  ASSIGN lo_data-&gt;* TO &lt;lt_data&gt;.\r\n\r\n* Get data from selected table\r\n  SELECT * FROM (p_tabnam)\r\n    INTO TABLE &lt;lt_data&gt;.<\/pre>\n<h2>Assign components (fields) of a structure dynamically<\/h2>\n<pre lang=\"abap\">LOOP AT &lt;lt_data&gt; ASSIGNING &lt;ls_data_line&gt;.\r\n  ASSIGN COMPONENT p_comp \r\n    OF STRUCTURE &lt;ls_data_line&gt; \r\n    TO           &lt;lv_data_field&gt;.\r\n  CHECK sy-subrc = 0.\r\n  IF &lt;lv_data_field&gt; IS INITIAL.\r\n    &lt;lv_data_field&gt; = p_value.\r\n  ENDIF.\r\nENDLOOP.<\/pre>\n<p>If you need to access more components of a structure in the loop, it might be a good idea to create a MACRO definition. In that case you can modify the above code snippet as the following:<\/p>\n<pre lang=\"abap\">DEFINE assign_value.\r\n  ASSIGN COMPONENT &amp;1 OF STRUCTURE &lt;ls_data_line&gt; TO &lt;lv_data_field&gt;.\r\n  IF sy-subrc = 0.\r\n    &lt;lv_data_field&gt; = &amp;2.\r\n  ENDIF.\r\nEND-OF-DEFINITION.\r\n\r\nLOOP AT &lt;lt_data&gt; ASSIGNING &lt;ls_data_line&gt;.\r\n  assign_value 'MSTAE' 'ZX'.\r\n  assign_value 'MHDHB' '365'.\r\n  assign_value 'KZKFG' 'X'.\r\n* ...\r\nENDLOOP.<\/pre>\n<h2>Append fields to your ITAB during runtime<\/h2>\n<pre lang=\"abap\">DATA:\r\n  lo_data_line  TYPE REF TO DATA,\r\n  lo_sdescr     TYPE REF TO cl_abap_structdescr,\r\n  lo_tdescr     TYPE REF TO cl_abap_tabledescr,\r\n  lt_components TYPE abap_component_tab.\r\n\r\nFIELD-SYMBOLS:\r\n  &lt;ls_component&gt; TYPE abap_componentdescr.\r\n\r\n* Create dynamic line of our itab\r\nASSIGN lo_data-&gt;* TO &lt;lt_data&gt;.\r\nCREATE DATA lo_data_line LIKE LINE OF &lt;lt_data&gt;.\r\n\r\n* We are creating components of the new\/updated ITAB\r\nAPPEND INITIAL LINE TO lt_components ASSIGNING &lt;ls_component&gt;.\r\n \r\n* We get description of the current itab\r\n&lt;ls_component&gt;-type ?= cl_abap_structdescr=&gt;describe_by_data_ref( lo_data_line ).\r\n* We assign some dummy name (will not be used anyhow)\r\n&lt;ls_component&gt;-name = 'DATA'.\r\n \r\n* Include all fields \r\n* If 'as_include' = abap_false, then all fields of the structure\r\n* would be added in our new itab as one field of type\r\n* \"structure\" with name 'DATA'\r\n&lt;ls_component&gt;-as_include = abap_true.\r\n \r\n* Now we're adding new component of type\r\n* LVC_T_SCOL - Internal table\r\nAPPEND INITIAL LINE TO lt_components ASSIGNING &lt;ls_component&gt;.\r\n&lt;ls_component&gt;-type ?= cl_abap_structdescr=&gt;describe_by_data( l_lvc_t_scol ).\r\n&lt;ls_component&gt;-name = 'CELL_COLORS'.\r\n \r\n* We create reference to a line description of the new itab\r\nlo_sdescr = cl_abap_structdescr=&gt;create( lt_components ).\r\n* ...we create reference to the new ITAB description\r\nlo_tdescr = cl_abap_tabledescr=&gt;create( lo_sdescr ).\r\n \r\n* Finally we create our new ITAB, where\r\n* 1. all fields from fieldcat are included\r\n* 2. New field of type LCV_T_SCOL is appended\r\nCREATE DATA lo_data TYPE HANDLE lo_tdescr.<\/pre>\n<h2>Call FORMs of other programs<\/h2>\n<p>Let&#8217;s assume you have a report called Z_REPORT and you want to call its FORM <em>get_data<\/em> which e.g. reads data from VBEP.<\/p>\n<pre lang=\"abap\">PERFORM get_data(Z_REPORT).<\/pre>\n<h2>Access variables from other programs<\/h2>\n<p>Your previous call caused required data to be loaded but the data is available in the Z_REPORT&#8217;s global variable &#8216;gt_vbep&#8217; only &#8211; how to access it?<\/p>\n<pre lang=\"abap\">DATA:\r\n  lv_variable(40) TYPE c.\r\n\r\nFIELD-SYMBOLS:\r\n  &lt;lt_vbep&gt; TYPE TABLE OF vbep,\r\n  &lt;ls_vbep&gt; TYPE VBEP.\r\n\r\nlv_variable = \u2018(Z_REPORT)gt_vbep\u2019.\r\nASSIGN (lv_variable) TO &lt;lt_vbep&gt;.\r\n\r\nLOOP AT &lt;lt_vbep&gt; ASSIGNING &lt;ls_vbep&gt;.\r\n  &lt;ls_vbep&gt;-TDUHR = \u2018235959\u2019.\r\nENDLOOP.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this article I&#8217;d like to present different possible approaches on accessing data and working with it dynamically.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"ABAP - Working with dynamicallly accessed data","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,15,9],"tags":[216,215,213,214,179,221],"class_list":["post-1357","post","type-post","status-publish","format-standard","hentry","category-abap","category-algorithms","category-development","tag-abap_componentdescr","tag-abap_component_tab","tag-cl_abap_structdescr","tag-cl_abap_tabledescr","tag-dynamic-programming","tag-oo-abap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-lT","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1357"}],"version-history":[{"count":4,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1357\/revisions"}],"predecessor-version":[{"id":1361,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1357\/revisions\/1361"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}