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: 

Add number of records to a field

Former Member
0 Kudos

I need to load data in BW ODS from flat file and want to populate one field ROWID which will hold record numbers starting from 1 if ODS is empty. This ROWID in ODS is used as Key field. I want to populate this ROWID based on number of records in flat file.

If there are already records in ODS and say last record number is 10, next record number will be 11 and will populate rests one by one.

So first I want to read ODS table if there are any records. If empty, ROWID will be populating from record 1 and if not empty, get the last ROWID and populate from next number.

Help me with the code please?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

If the select count (*) is successfull then add 1 to the count then move it to the rowid column.

SELECT COUNT(*) INTO V_COUNT FROM TABLENAME.

IF SY-SUBRC NE 0.

* If no records found then set the counter to 1.
  v_count = 1.

ELSE.

* If the record is found then set the row id of the table by adding 1.
  TABLENAME-ROWID = V_COUNT + 1.

ENDIF.

**If you want to insert multiple records within a loop.

LOOP AT ITAB.

* Increment the counter.
  V_COUNT = V_COUNT + 1.
  
* Set the row id.
  TABLENAME-ROWID = V_COUNT.

ENDLOOP.

Hope this helps.

Thanks

Naren

3 REPLIES 3

Former Member
0 Kudos

Hi,

Use SELECT COUNT(*) to get the number of rows from the table.

SELECT COUNT(*) INTO v_count FROM tablename.

IF sy-subrc ne 0.

* No record found set the counter to 1
  v_count = 1.

ENDIF.

Thanks

Naren

0 Kudos

Thanks Naren.

If there are records, what would be the code?

Former Member
0 Kudos

Hi,

If the select count (*) is successfull then add 1 to the count then move it to the rowid column.

SELECT COUNT(*) INTO V_COUNT FROM TABLENAME.

IF SY-SUBRC NE 0.

* If no records found then set the counter to 1.
  v_count = 1.

ELSE.

* If the record is found then set the row id of the table by adding 1.
  TABLENAME-ROWID = V_COUNT + 1.

ENDIF.

**If you want to insert multiple records within a loop.

LOOP AT ITAB.

* Increment the counter.
  V_COUNT = V_COUNT + 1.
  
* Set the row id.
  TABLENAME-ROWID = V_COUNT.

ENDLOOP.

Hope this helps.

Thanks

Naren