Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Filling fields in work area and counting fields

Former Member
0 Kudos

Hi,

1. I would like to know how to get the number of fields in an internal table by passing the table as input.

2. Then,

do 4 times.

move table-record index <..> to work_area-field1.

enddo.

the four records in table are four fields in the work area. Using the do loop i must fill the work area with data from the table. The workarea field names are generated dynamically by the program. Can any one help me with some suggestions and sample codes.

Thanks in advance.

Regards,

Kathir

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

I got the first problem solved. Just tell me how to append the data in <FS> (type any) to a work area <FS_1>

that has 4 fields. Using a loop i am supposed to append data into the work area <FS_1> first and finally update it to the table <FS_data>. And i am using field symbols for all these operations.Hope i am clear. Please help me in this regard.

Thanks in advance.

Kathir.

6 REPLIES 6

andreas_mann3
Active Contributor
0 Kudos

Hi Kathir,

DO.

ASSIGN COMPONENT SY-INDEX OF STRUCTURE tab TO <F>.

IF SY-SUBRC <> 0.

EXIT.

ELSE.

add 1 to count. "1)

*2)

if count = x.

assign COMPONENT count OF STRUCTURE wa TO <W>.

*move table-record index <..> to work_area-field1.

<w> = <f>.

endif.

enndo.

grx Andreas

P.S.

merry christmas and a happy new year to all SDN's

SAP's and ABAP's

Former Member
0 Kudos

Hi Andreas,

My first problem is solved. Lte me put things clear for the second case,

I created a table (<FS_DATA>) dynamically and assigned it to a new symbol <FS_1>.

Now,

do count times.

move table-record1 index <..> to <FS_1>-field1.

enddo.

now table-record1 should go to <FS_1>-Field1,

table-record2 should go to <FS_1>-Field2,

The Problem is that the number of fields in the work area is not fixed. So i need a generalized code such that if there 4 fields in <FS_1> then those four fields should be filled automatically using a loop. ie <FS_1>-Field1, <FS_1>-Field2...are to be referred in a dynamic manner.

Thanks in advance.

Kathir.

0 Kudos

Hi Kathirvel,

Andreas has given the code for changing work area, as you desire. If you see the


DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <structure_name> to <FS>
if sy-subrc <> 0.
   exit.
endif.
ENDDO.

What the above code does is, SY-INDEX starts with 1, and assigns the first field of <structure_name> to the field-symbol and this continues as long as that assign does not fail. So if your structure keeps on varying, it does not matter. At the point where it fails, it will exit.

In case your question is answered, make sure you give Andreas his deserved points. It is Christmas after all.

Regards,

Subramanian V.

P.S. - Merry Christmas to everyone here and a HAPPY NEW YEAR too.

Former Member
0 Kudos

Hi,

I got the first problem solved. Just tell me how to append the data in <FS> (type any) to a work area <FS_1>

that has 4 fields. Using a loop i am supposed to append data into the work area <FS_1> first and finally update it to the table <FS_data>. And i am using field symbols for all these operations.Hope i am clear. Please help me in this regard.

Thanks in advance.

Kathir.

0 Kudos

I shall take an example and let you know how to do this. I am guessing two things , let me know if I am correct.

a) You are developing a report

b) This report is about months which are stored as records and now to be displayed as columns.

Consider an internal table like this:


<b>LT_INTERNALTABLE</b>
 -------------------------------
| <b>Type</b>   | <b>Month</b>  |  <b>Quantity</b> |
 -------------------------------
| Sales  | Jan    |  20       |
| Sales  | Feb    |  30       |
| Sales  | Mar    |  10       |
 -----------------------------
| Inven  | Jan    |  45       |
| Inven  | Feb    |  60       |
| Inven  | Mar    |  75       |
 -----------------------------
| Distr  | Jan    |  35       |
| Distr  | Feb    |  45       |
| Distr  | Mar    |  60       |
 -----------------------------

There are nine records in the internal table which you have fetched from the database. However, you do not want to show this in the given format, but you want to show it something similar to this:


<b>Sales</b>
------
Jan     Feb       Mar
20      30        10

<b>Inventory</b>
---------
Jan     Feb       Mar
45      60        75

<b>Distribution</b>
------------
Jan     Feb       Mar
35      45        60

Let me know if I have got this part correct.(I would also wish if you could state your problem as clearly as possible, as I have stated my assumption ). This could have additional fields like MATNR(Material number) etc.. so that the number of entries in the above table will be more than 1.

Coming back to your query, if this is what you intend to do, then it is a slightly tricky approach.

You mentioned that you have created the internal table in runtime. If you are having any problems with that refer to this :

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

Let us say the original internal table with data is

a) <b>LT_INTERNALTABLE</b> - Original Internal Table

b) <b><FS_INTERNALTABLE></b> - Dynamic Internal Table that you have created

c) <b><FS_WORKAREA></b> - Work area of type <FS_INTERNALTABLE>

d) <b><FS_FIELD></b> - Field symbol which accesses each field of <FS_INTERNALTABLE>

e) <b><FS_WORKAREA></b> has first field as TYPE


count = 2.  "Since FS_WORKAREA has first field as type,
            "need to start putting values from 2 onwards

SORT LT_INTERNALTABLE BY TYPE.
LOOP AT LT_INTERNALTABLE INTO WA_INTERNALTABLE.

   AT NEW TYPE.
   <FS_WORKAREA>-TYPE = WA_INTERNALTABLE-TYPE.
   ENDAT.

   ASSIGN COMPONENT count of STRUCTURE <FS_WORKAREA> TO <FS_FIELD>.
   if sy-subrc = 0.
      <FS_FIELD> = WA_INTERNALTABLE-QUANTITY.
      count = count + 1
   else.
      clear count.
      APPEND <FS_WORKAREA> TO <FS_INTERNALTABLE>.
   endif.

ENDLOOP.

Let us know if this works, or have I gone tangent.

Regards,

Subramanian V.

Former Member
0 Kudos

Yes.. I got the problem fixed. Thanks a lot Andreas and Subramanian.