Generate number using Identity

Asked

Viewed 174 times

3

Is it possible to generate a number before writing the record to the database? Use field identity do SQL Server 2014. Work with Delphi Berlin.

  • 2

    What kind of number, a random number?

  • 1

    Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

4

In general, it may not, at least not IDENTITY normal.

You can use some techniques to help with this. One of them is to generate the insertion to get the number and then update with the actual data. But it is very problematic, need to do the application remove when you no longer need, can create holes in the numbering, have to turn off restrictions, anyway, do not recommend.

Another is to have a table where you control the numbering on your own and will reserve or release the reservation if the insertion is not effective. It’s not simple, but it’s a better solution.

Of course you can take the number after insertion, which is the most common technique and usually works well in most situations. Every idea of the IDENTITY is to have a centralized control of the database.

Another way is to use a UUID/GUID as the primary key, so you don’t need to generate anything in the database, you already know beforehand which identifier always needs to go to the database.

Should interest:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.