Application Development and Automation 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: 
Read only

Traverse Columns in Internal Table Programatically

Former Member
0 Likes
1,102

In SAP BW, I have an internal table "COMM_STRUCTURE" composed of 52 columns, one for each week of the year.

Rather than appending 52 times to RESULT_TABLE, is there a way to do it in a loop, and traverse each column of a row programatically?

The codes looks like this. I want to eliminate the repeated lines. Thanks for any help.

FORM compute_data_field

TABLES MONITOR STRUCTURE RSMONITOR "user defined monitoring

RESULT_TABLE STRUCTURE /BIC/VIC_BUD_JCT

USING COMM_STRUCTURE LIKE /BIC/CSIS_MONBUD

RECORD_NO LIKE SY-TABIX

RECORD_ALL LIKE SY-TABIX

SOURCE_SYSTEM LIKE RSUPDSIMULH-LOGSYS

ICUBE_VALUES LIKE /BIC/VIC_BUD_JCT

CHANGING RETURNCODE LIKE SY-SUBRC

ABORT LIKE SY-SUBRC. "set ABORT <> 0 to cancel update

RESULT_TABLE = ICUBE_VALUES.

RESULT_TABLE-/BIC/IO_BUDINC = COMM_STRUCTURE-/BIC/IO_BAMT01.

RESULT_TABLE-CALMONTH = 200401.

APPEND RESULT_TABLE.

RESULT_TABLE-/BIC/IO_BUDINC = COMM_STRUCTURE-/BIC/IO_BAMT02.

RESULT_TABLE-CALMONTH = 200402.

APPEND RESULT_TABLE.

6 REPLIES 6
Read only

Former Member
0 Likes
850

Yes, you can use Field Symbols <fs>

Read only

Former Member
0 Likes
850

Hi Jerry,

Please follow this topic and you will find how to achieve the traverse.

Regards,

Srinivas

Read only

Former Member
0 Likes
850

Thanks for the responses. I don't have much experience with ABAP, so I appreciate your patience. I still can't get it to work. The following, which is trying to turn the 52 week columns (ignoring the first column) into 52 rows, doesn't write anything.

Can someone see what my error is?

PROGRAM UPDATE_ROUTINE.

FIELD-SYMBOLS <FS> TYPE ANY.

FORM compute_data_field

TABLES MONITOR STRUCTURE RSMONITOR "user defined monitoring

RESULT_TABLE STRUCTURE /BIC/VIC_BUD_JCT

USING COMM_STRUCTURE LIKE /BIC/CSIS_MONBUD

RECORD_NO LIKE SY-TABIX

RECORD_ALL LIKE SY-TABIX

SOURCE_SYSTEM LIKE RSUPDSIMULH-LOGSYS

ICUBE_VALUES LIKE /BIC/VIC_BUD_JCT

CHANGING RETURNCODE LIKE SY-SUBRC

ABORT LIKE SY-SUBRC. "set ABORT <> 0 to cancel update

RESULT_TABLE = ICUBE_VALUES.

DO.

ASSIGN COMPONENT SY-INDEX OF STRUCTURE COMM_STRUCTURE TO <FS>.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

IF SY-INDEX > 1.

RESULT_TABLE-/BIC/IO_BUDAMT = <FS>.

RESULT_TABLE-CALMONTH = SY-INDEX.

APPEND RESULT_TABLE.

ENDIF.

ENDDO.

RETURNCODE = 0.

ABORT = 0.

ENDFORM.

Read only

0 Likes
850

Hi,

At first sight your code looks okay but I am doubting at the usage of sy-index as a counter ...

I would suggest making following changes:

- define a local counter

data: w_fieldindex type i value 1.

- use the counter in stead of sy-index and of course you need to increase the counter by hand

- you can get ridd of the test 'if sy-index > 1' ... Just give the counter already the default value for the second index lookup so you can ommit that if statement (should give some performance)

- Not sure but doesn't the index starts at 0 - so first field is index 0 (check that ... so then the default index to start can be set to 1)

Hope this helps,

Steven

Read only

0 Likes
850

Hello Jerry,

Sorry for the delayed response to this question.

You are using the <i>ASSIGN COMPONENT OF</i> option in your code here. Have you checked whether the fields -/BIC/IO_BAMT01 , -/BIC/IO_BAMT02....are consecutive and contiguous in the structure COMM_STRUCTURE (which is of type /BIC/CSIS_MONBUD) ? If not then you will have problems. Also, the statement

RESULT_TABLE-CALMONTH = SY-INDEX.

seems to be incorrect. You want tthe CALMONTHs to be in the format 2004xx, right? If yes, then the above statement will not do. I have altered your code a little hwere necessary. Please see if it works. If you still find some problems, please give the structure of /BIC/CSIS_MONBUD.

Also please confirm whether your original program is working as intended.

====================================================================


PROGRAM UPDATE_ROUTINE.
FIELD-SYMBOLS <FS> TYPE ANY.

FORM compute_data_field TABLES MONITOR        STRUCTURE RSMONITOR 
                               RESULT_TABLE   STRUCTURE /BIC/VIC_BUD_JCT
                         USING COMM_STRUCTURE LIKE      /BIC/CSIS_MONBUD
                               RECORD_NO      LIKE      SY-TABIX
                               RECORD_ALL     LIKE      SY-TABIX
                               SOURCE_SYSTEM  LIKE      RSUPDSIMULH-LOGSYS
                               ICUBE_VALUES   LIKE      /BIC/VIC_BUD_JCT
                      CHANGING RETURNCODE     LIKE      SY-SUBRC
                               ABORT          LIKE      SY-SUBRC. "set ABORT <> 0 to cancel update"

  DATA TEMP_CALMONTH(6) TYPE N. 
  
  TEMP_CALMONTH = '200401'.  

  RESULT_TABLE = ICUBE_VALUES.

  DO.
    ASSIGN COMPONENT SY-INDEX OF STRUCTURE COMM_STRUCTURE TO <FS>.
    IF SY-SUBRC <> 0.
      EXIT.
    ENDIF.

    IF SY-INDEX > 1.
      RESULT_TABLE-/BIC/IO_BUDAMT = <FS>.
      MOVE SY-INDEX TO TEMP_CALMONTH+4(2).  
      RESULT_TABLE-CALMONTH = TEMP_CALMONTH.
      APPEND RESULT_TABLE.
    ENDIF.
  ENDDO.

  RETURNCODE = 0.
  ABORT = 0.

ENDFORM. 

====================================================================

The DO..ENDDO loop above can also be coded in a different way -


DATA : TEMP_INDEX(2) TYPE N,
       TEMP_MONBUD(29) TYPE C VALUE 'COMM_STRUCTURE-/BIC/IO_BAMT'.
	
DO.
  MOVE SY-INDEX TO TEMP_INDEX.
  CONCATENATE TEMP_MONBUD 
              TEMP_INDEX
         INTO TEMP_MONBUD.
  ASSIGN COMPONENT TEMP_MONBUD OF STRUCTURE COMM_SRTUCTURE TO <FS>.
* Rest of the code is same as above. Here, you do not have to know the actual structure of COMM_STRUCTURE,
so long as you know that there will be fields with the name /BIC/IO_BAMTxx.  
ENDDO.

Hope to see your problem resolved ASAP. All the best,

Regards,

Anand Mandalika.

Read only

0 Likes
850

Thanks, Anand. I had multiple problems with my code, and your suggestions were on target for a lot of my problems. Thanks again.