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: 

Generate IDs for different tables

Former Member
0 Kudos

Hi SAP Community,

I have a issue relating to ID-generation.

I have different tables with their own primary keys/IDs e.g. zautopool-carID, zcitys-cityID..

Now, I want to write a (general) method which should generate simple IDs for all of my tables. They begin with 1 and they get incremented with 1 and so on.

So, what can you suggest me, which technology should I use, so that the ID generator is general (That means, it should be able to generate a carID, a cityID with the same code.)

I tried it this way:

Firstly I declared a Fieldsymbol <fsID>.
Then : ASSIGN ls_autopool-carid TO <fsID>
Then I wrote this method:

method GENERATE_ID.


data: lastid TYPE zmwa_id.

select count(*) <fsID> from zautopool into lastid.           ### Here, I wanted to use <fsID> as a reference to ls_autopool-carid 
endselect.                                                                       ### My goal is that this Select-statement and the following ones can                                                                                
### handle all IDs.
*Algorithm to increment LastID.
select carid
from zautopool
WHERE carid = (lastid + 1)
INTO nextID.

IF nextID = ' '.
  newid = lastid + 1.
ELSE.
  newid = nextID + 1.
ENDIF.

Thank you for your ideas.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Better solution for your issue is generate the number range and assign the number range object to it. Everything system will take of it.

For more information about number range creation go through the below link.

http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=50004015.

3 REPLIES 3

Former Member
0 Kudos

Hi,

Better solution for your issue is generate the number range and assign the number range object to it. Everything system will take of it.

For more information about number range creation go through the below link.

http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=50004015.

0 Kudos

Thanks but this is too much effort for a small programm.

I don't need any variable to hold the year, company or subobject etc...

Isn't there a simplier solution?

0 Kudos

select * into table t_table from ztable.

describe t_table lines l_id.

l_id = l_id + 1.