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: 

How to "auto" generate "Primary Key" in custom table?

Former Member
0 Kudos

Hi Folks,

Requirement:

I need a function module or program that can create automatically a primary key.

Scenario:

I have a program that creates an entries and save it to a custom table at the same time, but a primary key should always be generated respective to the entries saved.

Example:

Material(MATNR) 4001001, Plant(WERKS) XX01, ID[Primary Key-auto generated]

(I'm thinking of a similar concept/logic on how a unique IDOC number is being generated or how SAP standard creates a unique entry)

I try to look for any SAP FM that can generate a PK, but there's no luck.

Thanks.

Regards,

Jun

1 ACCEPTED SOLUTION

ThomasZloch
Active Contributor
0 Kudos

Try function module GUID_CREATE (or dozens of similar ones).

Thomas

9 REPLIES 9

ThomasZloch
Active Contributor
0 Kudos

Try function module GUID_CREATE (or dozens of similar ones).

Thomas

0 Kudos

Hi,

Incremental auto PK ID creation is most likely preferred. So that when viewed in SE16, the entries are already arranged.

0 Kudos

Then you could introduce a new number range object and pull numbers from the stack, see tx SNRO.

Thomas

0 Kudos

please check these options

use number range object.

increment the number in the table maintenance generator events '05'.

go through these links

links:[http://help.sap.com/saphelp_nw04/Helpdata/EN/2a/fa0331493111d182b70000e829fbfe/content.htm]

links:[http://www.sapnet.ru/viewtopic.php?p=731]

links:[http://wiki.sdn.sap.com/wiki/display/Snippets/AlphaNumericNumberRange(0000toZZZZ)]

0 Kudos

Hi Keshu and All,

The links are helpful, however:

#1. I don't have authorization to create a new object in transaction SNRO, but the way I see the objects in SNRO is just for defining the length(number of characters) for the ranges that will be use if I'm not mistaken.

#2. FM NUMBER_GET_NEXT - yes it can populate incremental unique entries but it's only occurring at runtime ?? So when the program get terminated it's not being saved nowhere.

So after if I use FM NUMBER_GET_NEXT, I always have to look for all the last primary key in my custom table, then target it as my starting point for creating a new PK. I think this will give a performance issue, any comments?

For better visualization, I have scenario:

1. (ZQ_CREATE_PK) Create unique incremental PK for material, batch, so on..

2. Append line/entries to ZQ_TABLE

3. Repeat #1 and #2

4. User exits the program

5. Back to 1 --> At this point, I need to get the "LATEST" PK then start this as a starting point again to generate PK and append it to ZQ_TABLE

I'm assuming that SAP has other SAP FMs related in creating this scenario. Similar to IDOC # creation..

Thanks.

0 Kudos

select entry from table.

lets say first the table is blank.

if i start my program, i will set the key as 0000001. and keep appending , and next time add 1 to the key each time before appending.

so data is gonna be like

0000001

0000002

0000003

etc.

you exit the program.

again start,

select data from table, get it in descending order, or you can directly get it by select max(fieldname) from the table.

again do the same thing as above,

keep adding 1 to the key..

0000003 + 1 = 0000004

append.

etc.

one more option can be using time stamp.

becuase time stamp will vary for each time.

kesavadas_thekkillath
Active Contributor
0 Kudos

Hi just check this thread , You will be more clear about the number range object.

Former Member
0 Kudos

Here's the resolution:

Former Member
0 Kudos

Thanks, all helpful.

Either a custom logic or SNRO and FM combination.