Skip to Content
author's profile photo Former Member
Former Member

Generating serial numbers for primary key field

I am updating a table through a screen.

The primary key field is a char type field. The value (serial numbers) for the primary key field has to be generated automatically, once if I create a new record.

I used the following logic:

  • mcode is the key field.

select max( mcode ) into value1 from zcodes.

mcode = value1 + 1. " the newly generate field would be incrementated by 1 to have this value.

problem: But since the key field is char type, maximum value is not fetched properly after some values.

How can I do this? suggest me a logic.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Nov 12, 2008 at 06:47 AM

    Create and use a number range object to generate sequential numbers for the key field. Use transaction SNRO to create a number range object.

    Edited by: Manoj V Kumar on Nov 12, 2008 12:17 PM

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 12, 2008 at 06:47 AM

    Explaain your question bit more clearly

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 12, 2008 at 10:16 AM

    Hi

    execute transaction SNRO

    enter the number range object you wnat to create: say "ZNUMRANGE".

    click create button.

    enter text for your number range.

    in the number length domain field, enter a domain (suppose ZLENGTH: create it) , this domain decides the lenget of the number generated. if the domain is of type numc 5 the number range will lie between 00001 to 99999.

    In warning % field enter 10.

    Save the number range object with buffering allowed.

    click on button : "number ranges"--> click button "change intervals" --> press + button.

    in the popup, enter "01" for No field, enter 000001 in from number or any number from which you want

    your number range to start. in to number enter 999999

    Save number range object.

    in your program, call function module "NUMBER_GET_NEXT"

    data lv_num type numc5. "(or as per the length of ur number range)

    CALL FUNCTION 'NUMBER_GET_NEXT'

    EXPORTING

    nr_range_nr = '01'

    object = 'ZNUMRANGE' "name of number range object

    IMPORTING

    NUMBER = lv_num

    EXCEPTIONS

    INTERVAL_NOT_FOUND = 1

    NUMBER_RANGE_NOT_INTERN = 2

    OBJECT_NOT_FOUND = 3

    QUANTITY_IS_0 = 4

    QUANTITY_IS_NOT_1 = 5

    INTERVAL_OVERFLOW = 6

    BUFFER_OVERFLOW = 7

    OTHERS = 8

    .

    IF sy-subrc <> 0.

    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    else.

    write: lv_num.

    ENDIF.

    every time u execute the program the variable lv_num will generate new number.

    remember that when you transport your program, number range has to be recreated in target system.

    Thanks

    Vishal Kapoor

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.