Use unique ID (auto-incrementing) in text document?

I’m looking or a way to use a unique ID in a text document (not in a spreadsheet, not even in a table).
I want to be able to reference existing IDs repeatedly in the text, but I also need a way to insert a unique new ID.
Usage could be for example for serial numbers, or similar.
I tried with auto-incrementing database fields, but did not really get this working.
Either I cannot see the current value in the normal text view (like when mail-merge inserts the data only when printing), and/or I don’t know how to reference the ID several times in the same document (without triggering the auto-increment).

I guess this must have been done before, but I could not find any working solution when I searched. Any pointers where to look at?

Thanks in advance.

You can use an own “Number range”. For the first use set the cursor where you want the ID and goto Insert > Fields > Other and then tab Variables. In the section Type select item “Number range”. In the section Name overwrite the content with your own identifier, e.g. ID. Click button Insert. Close. You should see a 1 in the document.

Move cursor to the next place which should get the next number. Again goto Insert > Fields > Others > Variables. Click on Type “Number range”. Now you should see your identifier ID in the section Select. Click it. Insert. Close. Another way to input the next number is to mark an existing one and copy&paste it to the target place.

To set a reference to such number goto Insert > Fields > Others and then tab Cross-Referencies. Click on ID in section Type. The right list shows all existing numbers in their paragraphs. To insert only the number itself as reference, use item Numbering in the section ‘Insert reference to’. In this section the wording “category” means the text of the paragraph, which is before the number, and the wording “Caption text” means the text of the paragraph, which is behind the number. But there is a short coming: You can only reference one number per paragraph.

If you need to reference two different numbers from the same paragraph, you can do this by defining bookmarks. Mark the number and then Insert > Bookmark. Give it a text, that you can identify the spot easily. Same for the second number. To reference such bookmark goto Insert > Fields > Others and then tab Cross-Referencies. But now click on Bookmarks in section Type and select item “Reference” in section ‘Insert reference to’ to get the marked number. Here too the numbers adapt themselves, when the underlying ID changes.

Thanks a lot, this is really useful information.
However, it does not solve my problem - I guess I was not clear enough in my description of what I want to do: I need the unique ID to exist not only within one document, but to be shared across a (large) number of independent documents. This is why I think I need some reference to an external (to the individual document), central data base which provides the ID.

@wd, a reference to an external is indeed what you need. Could you update your question to make this point more clear?

To use a unique identifier for each document, you will need to register each identifier outside the document, or use a method that determines a unique identifier based on the properties or content of the document itself. A simple example of the former is the file name which is unique within the file system and is available as a ‘variable’ inside the document. An example of the latter is a checksum calculated over the contents of the file. However, this checksum changes with every change in the document and is not sequential.

An abstract solution would be: use a register to connect each document to its number, e.g. a database with a table with auto-incrementing numbers and the document’s path and file name. As you noticed, you must register once, and then reference the number by querying for the number belonging to the exact file. A macro of some sort could come in handy here, which is best saved in a document template.

Another abstract solution could be: use a method to determine the ID, for example by using a text file with just the previously used ID, and incrementing the number, and storing the number inside the document as a document property. (Again, this is probably a task for a macro.) You can then reference the stored number in the document content.