Creating a dynamic internal table in a dynamic ABAP table

Source: Internet
Author: User

Creating a dynamic internal table in a dynamic ABAP table

These two days made such a demand, which requires the user to enter the wage item (T512W-LGART) to find the data, and output to ALV, the requirement is that the output followed the input change. Because the configuration information of the wage item may change, the internal table used to store the data to be displayed cannot be known in advance, so you have to seek the construction method of the dynamic internal table.
In fact, there are also many examples on the Internet, but they all lack instructions. After a long morning, I learned something and wrote it out to share it with you.
Report z_dt2.
Type-pools: ABAP.
* Take t512w as an example to build a dynamic internal table
Tables: t512w.
* Field-symbols is a flag. You can think of it as a pointer. In the future, the created internal tables will be accessed through them.
Field-Symbols: [less than sign] dyn_table> type standard table,
[Minor sign] dyn_wa>,
[Minor sign] dyn_field>.
* A dy_table and a dy_line type are defined here as references, but to data does not understand what it means. I think it should be null.
Data: dy_table type ref to data,
Dy_line type ref to data,
* Used to store the dynamic internal table structure to be built
IFC type lvc_t_fcat,
Xfc type lvc_s_fcat.
Data: Begin of t_lgart occurs 0,
Lgart like t512w-lgart,
End of t_lgart.
Select-options: s_lgart for t512w-lgart.

Start-of-selection.
* First, enter the lgart that meets the user input in the database into the inner table. Each column of the dynamic inner table constructed in the future is each row in the lgart.
Select lgart into t_lgart-lgart from t512w where lgart in s_lgart and molga = '28 '.
Append t_lgart.
Endselect.
* Reads the inner table t_lgart cyclically, and builds the inner table for each row.
Loop at t_lgart.
* Here, I am a little lazy, because the salary item contains data such as/103, And/cannot be listed on the left. You can write a replacement logic by yourself, replace/with characters such.
Check t_lgart-lgart + 0 (1) ne '/'.
* The column name uses the lgart value.
Xfc-fieldname = t_lgart-lgart.
Xfc-datatype = 'C '.
Xfc-inttype = 'C '.
Xfc-intlen = 6.
Xfc-Decimals = 0.
Append xfc to IFC.
Endloop.
* Call a method to construct a dynamic internal table. The input is the structure to be constructed, and the output is a dy_table. It has been defined before, but cannot be accessed directly.
Call method cl_alv_table_create => create_dynamic_table
Exporting
It_fieldcatalog = IFC
Importing
Ep_table = dy_table.
* Set <dyn_table> to dy_table
Assign dy_table-> * to <dyn_table>.
* The following statement creates a <dyn_table> workspace work area.
Create data dy_line like line of <dyn_table>.
* Similarly, set pointers.
Assign dy_line-> * to <dyn_wa>.

Therefore, an inner table and a workspace are created dynamically. Because the inner table created in this method does not have the header line, a workspace is required.

The following is a method for getting a number or assigning a value:

* In this row, xxxx1 is the column name.
Assign component xxxx1 of structure <dyn_wa> to <dyn_field>.
* This row is used to give a value to the column in <dyn_wa>.
<Dyn_field> = xxxx2.
* Append can be used to add the row to the table.
Append <dyn_wa> to <dyn_table>.

as described above, internal tables can be dynamically built. This method is mainly used when the table structure cannot determine the number of columns and the number of columns, especially when it is related to information configuration, you can only use this method. Otherwise, the Program cannot be used.
I am a beginner in learning and hope to help you. I hope you can correct me and learn from me if you have any mistakes!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.