Copyright 2022 - BV TallVision IT

When you design a data model, it's more than likely you will need a unique number for your key fields. Whenever a new number is required, you could select on your own table and find the largest number, add 1 to that and your new key field value is ready. SAP caters for a bit more powerful and quicker "number ranging" than that. A number range is DDIC object that holds information on what new number to pick next. It can hold several "ranges" for your key field (e.g. related to a document type).

How does that work ?

Transaction SNRO or menu: R/3 => ABAP/4 Workbench => Development => Other tools => Number ranges. Try object DEBITOR (customer) if you like. Pretty soon the word intervals pops up. An interval is a possible value range, indicating "from - to". It's also called NR_RANGE_NR (see example).

Internal and external number ranges: when the end user wants to create a new object (use a new key field value), a value from the external interval(s) could be used (supplied by the end user) or a value from the internal interval(s) could be populated by the system. So key fields from an external system can be used - as long as they are within the range specified on the (external) number range.

Get a new number

In your ABAP you would use a function module NUMBER_GET_NEXT to start off this functionality. In the case of a customer number, this would look like this:

CALL FUNCTION 'NUMBER_GET_NEXT'
  EXPORTING
    OBJECT      = 'DEBITOR'
    NR_RANGE_NR = T077D-NUMKR
  IMPORTING
    NUMBER      = KNA1-KUNNR
    RETURNCODE  = RETCODE
  EXCEPTIONS
    INTERVAL_NOT_FOUND      = 01
    NUMBER_RANGE_NOT_INTERN = 02
    OBJECT_NOT_FOUND        = 03.

In the example above, the field KNA1-KUNNR will be filled with a new number. The value of T077D-NUMKR is a 2-character code that identifies the number range interval that should be used. 

The subobject

The subobject is a field of which the domain is specified in the number range object, allowing the number range object to maintain separate number ranges per "object". Thus it is possible to have separate number ranges per document type. These "sub-ranges" can even have overlapping number ranges, since the document type itself is part of the key. Have a glance at number range CO_RCL_DOC (Reconciliation ledger document number range) for an example.

Including the year could be useful too, by setting a flag in the header level of the number range. Every number range number (or interval if you like) will have a year number on it. If you want the same year number in your key field, you could start the interval(s) with that, e.g. 201700001 to 201799999 for year 2017.