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

Table Fill logic

Former Member
0 Likes
2,462

Can someone help me with some logic. The details are as follows:

I want to fill the table with records for 10 years from 01012008. Thanks

Table 1

Main_id u2013 Key field

Type u2013 Key field

No of units

Record Example

Main id: ABC

Type: Bottle

No of units: 20

Table 2

Main_id u2013 Key field

Type u2013 Key field

Date u2013 Key field

No of units

Record Example (for the record from table 1above there would be 3650 records)

Main id: ABC

Type: Bottle

Date: 01012008

No of units: 20

Main id: ABC

Type: Bottle

Date: 01012008

No of units: 20

etc

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,423

if there are 10 entries in table1.acc to ur req, i assume that these 10 will be in TABLE2 along wth date.

be clear on the date u need to pass.

data: itab1 type standard table of TABLE1,

itab2 type stabdard table of TABLE2.

data: v_dat type dats.

SELECT * from table 1 into table itab1.

loop at itab1 into wa_itab1.

wa_itab2-main_id = wa_itab1-main_id.

wa_itab2-type = wa_itab1-type.

wa_itab2-no.of units = wa_itab1-no.ofunits.

wa_itab2-date = v_dat. "pass the initialized date

APPEND wa_itab2 to itab2.

v_dat = v_dat + 1. " increment the date

endloop.

modify TABLE2 from itab2.

Edited by: Kalyan Chakravarthi on Jan 30, 2009 11:15 AM

Can someone help me with some logic. The details are as follows:

I want to fill the table with records for 10 years from 01012008. Thanks

Table 1

Main_id u2013 Key field

Type u2013 Key field

No of units

Record Example

Main id: ABC

Type: Bottle

No of units: 20

Table 2

Main_id u2013 Key field

Type u2013 Key field

Date u2013 Key field

No of units

Record Example (for the record from table 1above there would be 3650 records)

Main id: ABC

Type: Bottle

Date: 01012008

No of units: 20

Main id: ABC

Type: Bottle

Date: 01012008

No of units: 20

etc

22 REPLIES 22
Read only

Former Member
0 Likes
2,423

Hi

From your question I understand that your requirement is to copy from table 1 to table 2 ,assigning the date field in table 2 from starting date 01012008 .

data: itab2 type table of table2 with header line,

st_dt like sy-datum value '01012008'.

select * from dbtable1 into corresponding fields of itab2. " copies all data from table 1

endselect.

if sy-subrc eq 4.

Message E000(YJK).

else.

loop at itab2.

itab2-date = st_dt.

modify itab2. " adds the date to the records

st_dt = st_dt +1. "increments date.

endloop.

endif.

Also corresponding fields is not a good practice. So you can use 2 itabs and improve the performance.

Hope this helps

Regards,

Jayanthi.K

Read only

0 Likes
2,423

Thanks for this my table2 is not a copy of all the fields just a small subset of fields (Main_id, Type, No of units) and I will add date. What amendments would I need to make to the original example?

Read only

0 Likes
2,423

Hi

data: itab2 type table of table2 with header line,
        st_dt like sy-datum value '01012008'.

select * from dbtable1 into table itab2. " copies all data from table 1. No need for endselect

if sy-subrc eq 4.
        Message E000(YJK).
else. 
        loop at itab2.
             itab2-date = st_dt.
             modify itab2. " adds the date to the records
             st_dt = st_dt +1. "increments date.
         endloop.
endif.

The above code copies all data from table1 into itab2.

Inside the loop for each entry you are filling in the date part.

Hope this helps Do tell if it works

Regards,

Jayanthi.K

Read only

Former Member
0 Likes
2,423

create an internal table itab1 with fields Main_id, Type, No of units.

create an internal table itab2 with Main_id, Type, No of units, date.(these are the 4 fields that r in ur table2)

now select Main_id, Type, No of units from table1 into itab1.

loop on itab1.

move main_id in itab1 to itab2.

move type in itab1 to itab2.

move no of units in itab1 to itab2.

and now add the date to itab2.

APPEND ITAB2.

so this will get populated along with tha date for each entry.

now, modify the table2 with itab2.

Read only

0 Likes
2,423

Thanks for this. I am not a seasoned ABAPer but just handed the short straw. Could you provide logic for the pseudo logic that you have given u2013 not sure statement to create the itab

Read only

Former Member
0 Likes
2,424

if there are 10 entries in table1.acc to ur req, i assume that these 10 will be in TABLE2 along wth date.

be clear on the date u need to pass.

data: itab1 type standard table of TABLE1,

itab2 type stabdard table of TABLE2.

data: v_dat type dats.

SELECT * from table 1 into table itab1.

loop at itab1 into wa_itab1.

wa_itab2-main_id = wa_itab1-main_id.

wa_itab2-type = wa_itab1-type.

wa_itab2-no.of units = wa_itab1-no.ofunits.

wa_itab2-date = v_dat. "pass the initialized date

APPEND wa_itab2 to itab2.

v_dat = v_dat + 1. " increment the date

endloop.

modify TABLE2 from itab2.

Edited by: Kalyan Chakravarthi on Jan 30, 2009 11:15 AM

Read only

0 Likes
2,423

Thanks very much. I have created the report with the code as below and have a couple of questions:

1. How can I amend the code so that my v_dat has a start value of 01.01.2008 u2013 the field in the table is data type DATS length 8

2. I face the following error - The field "WA_ITAB1" is unknown, but there is a field with the similar name "ITAB1". "ITAB1". How do I resolve this

3. I cannot see the loop ending do I also need to provide an end date as part of my routine this is fixed my start date is 01.01.2008 and end date is 31.12.2020 can you advise

REPORT REPORT1.

data: itab1 type standard table of TABLE1,

itab2 type standard table of TABLE2.

data: v_dat type dats.

SELECT * from TABLE1 into table itab1.

loop at itab1 into wa_itab1.

wa_itab2-main_id = wa_itab1-main_id.

wa_itab2-type = wa_itab1-type.

wa_itab2-no.of units = wa_itab1-no.ofunits.

wa_itab2-date = v_dat. "pass the initialized date

APPEND wa_itab2 to itab2.

v_dat = v_dat + 1. " increment the date

endloop.

modify TABLE2 from itab2.

Edited by: Niten Shah on Jan 30, 2009 1:30 PM

Read only

0 Likes
2,423

Sorry to be a pain can someone assist me with this thanks

Read only

0 Likes
2,423

I don't fully understand what your goal is.. but the following code will correct a lot of your questions.


DATA: wa_itab1   LIKE table1.
DATA: wa_itab2   LIKE table2.
DATA: itab1 TYPE STANDARD TABLE OF wa_itab1,
      itab2 TYPE STANDARD TABLE OF wa_itab2.
DATA: v_dat TYPE d VALUE '20080101'.

SELECT * FROM table1 INTO TABLE itab1.

LOOP AT itab1 INTO wa_itab1.
  wa_itab2-main_id = wa_itab1-main_id.
  wa_itab2-type = wa_itab1-type.
  wa_itab2-no.of units = wa_itab1-no.ofunits.
  wa_itab2-date = v_dat. "pass the initialized date
  APPEND wa_itab2 TO itab2.
  v_dat = v_dat + 1. " increment the date
  IF vdat GT '20201231'.
    EXIT.
  ENDIF.
ENDLOOP.

MODIFY table2 FROM itab2.

The LOOP will stop automatically at the end of itab1, OR when you've written a record for every date from 1/1/2008 to 12/31/2020.

Read only

0 Likes
2,423

Thanks for this I now get the message:

Type "WA_ITAB1" is unknown

The code is below and I have commented downwards from the point that I get the error message

          • Not sure if it makes a difference my table name is as follows /BIC/Table1*********

****************

DATA: wa_itab1 LIKE table1.

DATA: wa_itab2 LIKE table2.

DATA: itab1 TYPE STANDARD TABLE OF wa_itab1,

  • itab2 TYPE STANDARD TABLE OF wa_itab2.

*DATA: v_dat TYPE d VALUE '20080101'.

*SELECT * FROM table1 INTO TABLE itab1.

*LOOP AT itab1 INTO wa_itab1.

  • wa_itab2-main_id = wa_itab1-main_id.

  • wa_itab2-type = wa_itab1-type.

  • wa_itab2-no.of units = wa_itab1-no.ofunits.

  • wa_itab2-date = v_dat. "pass the initialized date

  • APPEND wa_itab2 TO itab2.

  • v_dat = v_dat + 1. " increment the date

  • IF v_dat GT '20201231'.

  • EXIT.

  • ENDIF.

*ENDLOOP.

*MODIFY table2 FROM itab2.

Edited by: Niten Shah on Jan 30, 2009 4:25 PM

Read only

0 Likes
2,423

Chances are because you don't have a DB Table called TABLE1 or TABLE2 defined in SE11.

You're going to need to define them, or define a structure for WA_ITAB1.

Read only

0 Likes
2,423

You'll also need a line above all this code that contains:


TABLES: Table1, Table2.  "to whatever tables you end up working with.

Read only

0 Likes
2,423

Thanks for this.

The tables have been defined but not sure if the naming convention makes a difference. The tables are generated (BI tables) and have a naming convention /BIC/TABLE1

Read only

0 Likes
2,423

Sounds like you're on a UNIX system and I'm not familiar with that at all.

Not sure what BI is either.

In order for you to address DB Tables within a Report type program, the table must be defined and

accessable through transaction SE11. I knew you hadn't done this as generally, custom tables begin

with either a Z or Y. Y generally don't go to production.

Now, if the TABLE1 and TABLE2 are Flat files, you can import them into your program and process

them with most of the code defined.

Read only

0 Likes
2,423

Thanks. Both tables are accessible from SE11.

Read only

0 Likes
2,423

Then as a guess..

TABLES: BIC/Table1, BIC/Table2.

or

TABLES: 'BIC/Table1', 'BIC/Table2'.

and when you select from them..

FROM BIC/TABLE1 or FROM 'BIC/TABLE1'

Edited by: Paul Chapman on Jan 30, 2009 11:06 AM

Read only

0 Likes
2,423

Thanks for this. I have managed to get rid of the errors, the table names were fine and I referred both wa_itab and itab to the table

the code is below. The problem now is that what I am expecting once I run this program is to copy the data from table1 into table2

When I run the report nothing happens can you advise. Do I need to Append or anything?

REPORT REPORT1.

DATA: wa_itab1 LIKE table1.

DATA: wa_itab2 LIKE table2.

DATA: itab1 TYPE STANDARD TABLE OF table1,

itab2 TYPE STANDARD TABLE OF table2.

DATA: v_dat TYPE d VALUE '20080101'.

SELECT * FROM table1 INTO TABLE itab1.

LOOP AT itab1 INTO wa_itab1.

wa_itab2-FIELD1 = wa_itab1-FIELD1.

wa_itab2-FIELD2 = wa_itab1-FIELD2.

wa_itab2-FIELD3 = wa_itab1-FIELD3.

wa_itab2-FIELD4 = v_dat. "pass the initialized date

APPEND wa_itab2 TO itab2.

v_dat = v_dat + 1. " increment the date

IF v_dat GT '20201231'.

EXIT.

ENDIF.

ENDLOOP.

MODIFY table FROM wa_itab2.

Read only

0 Likes
2,423

Might help if you use debug and see if you are getting anything from your select into ITAB1.

Also

MODIFY table FROM wa_itab2.

likely should be

MODIFY table2 FROM TABLE itab2.

Edited by: Paul Chapman on Jan 30, 2009 11:49 AM

Read only

0 Likes
2,423

Thanks when I use:

MODIFY table2 FROM itab2.

I get the error message u201CYou cannot use an internal table as a work areau201D

Read only

0 Likes
2,423

Try this one

MODIFY table2 FROM TABLE itab2.

Read only

0 Likes
2,423

Thanks for all your help. I checked the table and data was populating - not as expected but will raise that as a seperate question. Have awarded points

Read only

0 Likes
2,423

Hi

Sorry wrong post.

Regards,

Jayanthi.K

Edited by: Jayanthi K on Jan 31, 2009 8:55 AM