{"id":7610,"date":"2025-08-24T16:08:44","date_gmt":"2025-08-24T10:38:44","guid":{"rendered":"https:\/\/www.sapewmhelp.com\/?question=how-to-create-internal-table-dynamically"},"modified":"2025-08-24T16:08:44","modified_gmt":"2025-08-24T10:38:44","slug":"how-to-create-internal-table-dynamically","status":"publish","type":"question","link":"https:\/\/www.sapewmhelp.com\/?question=how-to-create-internal-table-dynamically","title":{"rendered":"How to create internal table dynamically ?"},"content":{"rendered":"<p>Internal tables are a core concept for storing structured data. You can create them dynamically at runtime using RTTS (Runtime Type Services).<\/p>\n<p>DATA: lo_descr TYPE REF TO cl_abap_structdescr,<br \/>\nlo_table TYPE REF TO cl_abap_tabledescr,<br \/>\nlr_data TYPE REF TO data,<br \/>\nlr_line TYPE REF TO data.<\/p>\n<p>&#8220;&#8212; Step 1: Define a structure dynamically<br \/>\nlo_descr = cl_abap_structdescr=&gt;create(<br \/>\nVALUE #( ( name = &#8216;FIELD1&#8217; type = cl_abap_elemdescr=&gt;get_c( 10 ) )<br \/>\n( name = &#8216;FIELD2&#8217; type = cl_abap_elemdescr=&gt;get_i( ) ) )<br \/>\n).<\/p>\n<p>&#8220;&#8212; Step 2: Create internal table description<br \/>\nlo_table = cl_abap_tabledescr=&gt;create( lo_descr ).<\/p>\n<p>&#8220;&#8212; Step 3: Create actual data object (the table)<br \/>\nCREATE DATA lr_data TYPE HANDLE lo_table.<br \/>\nASSIGN lr_data-&gt;* TO FIELD-SYMBOL(&lt;itab&gt;).<\/p>\n<p>&#8220;&#8212; Step 4: Create a line type and assign<br \/>\nCREATE DATA lr_line TYPE HANDLE lo_descr.<br \/>\nASSIGN lr_line-&gt;* TO FIELD-SYMBOL(&lt;wa&gt;).<\/p>\n<p>&#8220;&#8212; Step 5: Fill dynamically created table<br \/>\n&lt;wa&gt;-field1 = &#8216;Hello&#8217;.<br \/>\n&lt;wa&gt;-field2 = 123.<br \/>\nAPPEND &lt;wa&gt; TO &lt;itab&gt;.<\/p>\n<p>&lt;wa&gt;-field1 = &#8216;World&#8217;.<br \/>\n&lt;wa&gt;-field2 = 456.<br \/>\nAPPEND &lt;wa&gt; TO &lt;itab&gt;.<\/p>\n<p>&#8220;&#8212; Step 6: Loop through the dynamic internal table<br \/>\nLOOP AT &lt;itab&gt; ASSIGNING &lt;wa&gt;.<br \/>\nWRITE: \/ &lt;wa&gt;-field1, &lt;wa&gt;-field2.<br \/>\nENDLOOP.<\/p>\n<h5>Key Points:<\/h5>\n<ol>\n<li>Use RTTS (Runtime Type Services) \u2192 <code>cl_abap_structdescr<\/code>, <code>cl_abap_tabledescr<\/code>, <code>cl_abap_elemdescr<\/code>.<\/li>\n<li>Create data objects at runtime with <code>CREATE DATA ... TYPE HANDLE<\/code>.<\/li>\n<li>Use field symbols (<code>&lt;itab&gt;<\/code>, <code>&lt;wa&gt;<\/code>) to access the data.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","question-category":[158],"question_tags":[178,357,177],"class_list":["post-7610","question","type-question","status-publish","hentry","question-category-abap","question_tags-abap","question_tags-internal-table","question_tags-sap"],"_links":{"self":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question\/7610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/question"}],"about":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7610"}],"wp:attachment":[{"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7610"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion-category&post=7610"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/www.sapewmhelp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fquestion_tags&post=7610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}