Skip to content

NUMBER_GET_NEXT

Draw the next sequential number from an SAP number range object — the standard approach for assigning unique IDs.

Purpose

NUMBER_GET_NEXT fetches and increments the current number in a number range interval. Number ranges are defined in transaction SNRO (number range objects) and SNUM (intervals). They guarantee uniqueness across concurrent sessions using database-level locking.

Use this instead of MAX() + 1 patterns — those fail under concurrent access.

Signature

Parameter Direction Type Description
NR_RANGE_NR Importing INRI-NRRANGENR Interval ID (usually '01')
OBJECT Importing INRI-OBJECT Number range object name
QUANTITY Importing INRI-QUANTITY How many numbers to draw (default: 1)
NUMBER Exporting INRI-NR The number returned

Exceptions:

Exception Meaning
INTERVAL_NOT_FOUND The interval does not exist
NUMBER_RANGE_NOT_INTERN The interval is external (user must enter the number)
OBJECT_NOT_FOUND The number range object does not exist
QUANTITY_IS_0 Zero quantity requested
QUANTITY_IS_NOT_1 Object does not allow bulk draws
INTERVAL_OVERFLOW The interval is exhausted
BUFFER_OVERFLOW Buffered number range buffer is full

Example — draw a single number

DATA lv_number TYPE inri-nr.

CALL FUNCTION 'NUMBER_GET_NEXT'
  EXPORTING
    nr_range_nr = '01'
    object      = 'ZMY_OBJECT'
  IMPORTING
    number      = lv_number
  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
    OTHERS                = 7.

IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  RETURN.
ENDIF.

" lv_number now holds the unique number, e.g. '0000001234'
DATA lv_doc_no TYPE inri-nr.
lv_doc_no = lv_number.

Example — draw multiple numbers at once

DATA: lv_first  TYPE inri-nr,
      lv_qty    TYPE inri-quantity VALUE 5.

CALL FUNCTION 'NUMBER_GET_NEXT'
  EXPORTING
    nr_range_nr = '01'
    object      = 'ZMY_OBJECT'
    quantity    = lv_qty
  IMPORTING
    number      = lv_first   " first of the 5 numbers
  EXCEPTIONS
    OTHERS = 1.

" Numbers lv_first through lv_first + 4 are now reserved

Setting up a number range object

  1. Run SNRO → create object (e.g. ZMY_OBJECT), set number length.
  2. In the object, go to Intervals and create interval 01 with a from/to range (e.g. 0000000001 to 9999999999).
  3. Activate the object.
  4. Transportable intervals: flag Transportable in the object definition if the interval should travel with the transport.

Common pitfalls

INTERVAL_OVERFLOW is permanent

Once exhausted, the interval cannot auto-extend. Monitor current levels with SNRO → Intervals and extend the to-number proactively.

Numbers are consumed even on rollback

Once NUMBER_GET_NEXT returns a number, it is consumed. Rolling back the transaction does not return the number to the range. This is by design — gaps in the sequence are normal and expected.

External number ranges

If NUMBER_RANGE_NOT_INTERN fires, the interval is configured as external — the calling program must supply the number. Check the interval in SNRO.

See also

  • Number ranges tutorial — creating objects, intervals, and transport
  • Transaction SNRO — number range object maintenance
  • Transaction SNUM — number range interval maintenance

Comments