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: 

Auto increment a field in a table

Former Member
0 Kudos

Hello all,

I have a table created in the data dictionary and have a field that needs to be incremented automatically when a new row is added. How do I achieve this? Also, what would happen when a row is deleted from the table? Would those numbers ever be reused?

Thanks in advance!

Narayana

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Narayana,

Following are the steps to do this.

1. You have to create/define a number range object for the field you are trying to increment. Go to transaction SNRO and create a appropriate number range object for your field. Most probably you wanna use a integer range. For that use the domain as NUMC4 or NUMC5 and the warning % as 2%.

2. Create an interval for the number range object. Go to the 'Number Range' tab on SNRO main screen. Then select 'Change Intervals'. Add a new interval '01'. and add the required number range to it.

3. In your ABAP program when you insert a new row to the database table, use the FM 'NUMBER_GET_NEXT'. Pass the values 01 and <name of number range object created> to the FM parameters 'NR_RANGE_NR' and 'OBJECT' respectively. The o/p parameter 'NUMBER' gives you the next number in the number range.

4. Pass this number to the field for which you want the value to be incremented.

Let me know if you face any issues in this.

Regards

Barada

Edited by: Baradakanta Swain on Dec 15, 2008 8:03 AM

3 REPLIES 3

Former Member
0 Kudos

Hi Narayana,

Following are the steps to do this.

1. You have to create/define a number range object for the field you are trying to increment. Go to transaction SNRO and create a appropriate number range object for your field. Most probably you wanna use a integer range. For that use the domain as NUMC4 or NUMC5 and the warning % as 2%.

2. Create an interval for the number range object. Go to the 'Number Range' tab on SNRO main screen. Then select 'Change Intervals'. Add a new interval '01'. and add the required number range to it.

3. In your ABAP program when you insert a new row to the database table, use the FM 'NUMBER_GET_NEXT'. Pass the values 01 and <name of number range object created> to the FM parameters 'NR_RANGE_NR' and 'OBJECT' respectively. The o/p parameter 'NUMBER' gives you the next number in the number range.

4. Pass this number to the field for which you want the value to be incremented.

Let me know if you face any issues in this.

Regards

Barada

Edited by: Baradakanta Swain on Dec 15, 2008 8:03 AM

Sandra_Rossi
Active Contributor
0 Kudos

In SAP, you can't use an "autoincrement" field, you must program "+1" in your program. A number range is only needed when your "field describes the database table" (for example, if it's the document number in the document table). If your field is only used to make unique a record (for example, items of a document), you may just increment it in the program.

Moreover, if the records are added manually (in SM30 for example), you may develop the "+1" via "events" (see http://help.sap.com/saphelp_nw2004s/helpdata/en/91/ca9f0ba9d111d1a5690000e82deaaa/frameset.htm). If a row is deleted, don't do anything (keep holes).

Your last question suggerates that you need a number range : you may customize the number range to stop at the end of numbers (programs should fail), or restart from zero. Your program has to check the number returned in case the number is already used in database table, and choose next number in that case (and so on).

former_member181995
Active Contributor
0 Kudos

This message was moderated.