0

I am using the answer of this question How to automatically generate unique id in sql server to create a custom id for a table.It worked perfectly.
Now I have a column which holds the values such as UID00000001 UID00000002 and so on. Suppose the last value in this column is UID00000003.Now I want to calculate the value for the row which hasn't been inserted yet via C# in one of my .aspx pages.In this case UID00000004. How can I achieve this value? Any help would be appreciated. Thank you.

Community
  • 1
  • 1
Elham Kohestani
  • 3,013
  • 3
  • 20
  • 29
  • Is it acceptable to show these values after the rows are actually saved? No one can tell what IDs you will receive when saving using identity or sequence, unless you are generating the identifiers in your application. This can happen if you are sure that only your application is performing inserts in that table. – Alexei - check Codidact Apr 08 '17 at 18:38
  • 1
    You can't calculate the value in advance because insert might fail, and if that happens you will get the wrong number, or some other process might insert a record between the calculation time and the time you actually do the insert. – Zohar Peled Apr 08 '17 at 18:53

2 Answers2

1

If you are not required to generate these identifier at database level (e.g. some other processes insert records there), you can pre-generate them within your application. Something like above:

class Generator
{
     public static int UniqueId = 0;

     public static int GetNextId()
     {
          return Interlocked.Increment(ref UniqueId);
     }
}

Then, your code can preallocate these identifiers and also format those strings. If multiple users access the same functionality, they will receive other identifiers. However, if one does not (successfully) performs a save operation, those identifiers will be lost.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • I don't need to do anything with this value. I just need it for some display purposes. Anyway, thank you for posting your valuable time. – Elham Kohestani Apr 08 '17 at 19:03
0

You need to execute this query to get the next identity which will be generated for the table:

SELECT IDENT_CURRENT('table_name')+1; 

For your case, it will have some other info concatenated with the next identity so the query will be like this:

SELECT 'UID' + RIGHT('00000000' + CAST(IDENT_CURRENT('table_name')+1 AS VARCHAR(8)), 8)

Of course you will need to write the C# code to send that query to the SQL Server.

Having said that, keep this in mind: When you get the value from that call and hold onto it, if during the time you are holding the value a record is inserted into that table, then the value is no longer the next value.

If you need the identiy value after a record is inserted in your application, please refer this answer.

Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • CodingYoshi please read the answer to the question which I have provided its link. This column is not an autoincrement column. – Elham Kohestani Apr 08 '17 at 18:44
  • Beside that I need this value just for display purposes I won't send this value again to sql server. – Elham Kohestani Apr 08 '17 at 18:46
  • @ElhamKohestani according to that answer it is an auto increment column (it is a combination of identity value and some other characters). – CodingYoshi Apr 08 '17 at 18:50
  • But in there the 'ID' is an auto increment, not 'UID'. So executing this SELECT IDENT_CURRENT('table_name')+1; will increment 'ID' or 'UID'? – Elham Kohestani Apr 08 '17 at 18:54
  • Ok I will try it once. – Elham Kohestani Apr 08 '17 at 18:55
  • This is ***NOT SAFE*** in a multi-user environment and will **lead to duplicates**! Do ***NOT*** do something like this! If more than one client is connected and runs this code at the same time, they all get **the same value** and thus will insert **duplicate new values** - do ***NOT*** do this, let the database handle unique values ! – marc_s Apr 08 '17 at 19:16
  • @marc_s The OP has already decided to do that and now wanted to get the value. I mentioned in my answer what could be the consequences. I don't think your downvote is fair... – CodingYoshi Apr 08 '17 at 19:18
  • @marc_s Thank you for your comment. I know this is not safe in multi-user environment but what I need is to only show the value for the user. I am not going to include this value in my insert statement. So actually I have left the insertion task for the SQL Server. – Elham Kohestani Apr 10 '17 at 13:35
  • @ElhamKohestani: just wonder if it's a good idea to potentially show the user *one* value, but then after inserting, the row gets another value ..... doesn't that cause more confusion than clarity? I would just **hide** the auto-increment value from the users entirely - or only show it once you've **actually** inserted the value.... (just my 2 cents from 30+ years of programming experience....) – marc_s Apr 10 '17 at 13:38
  • @marc_s You are absolutely right but I don't have any other option for now. Can we do any row lock stuff inside our application ? Or do you have other option ? – Elham Kohestani Apr 10 '17 at 13:43
  • @ElhamKohestani: "row lock" makes me cringe every time I hear it .... don't even **start** doing stuff like that!! I still don't understand ***why*** it's so bloody important to show a **meaningless** value like an auto-increment to the user *before* the row has actually been inserted.... I would try to really find out if you absolutely **MUST** support that feature..... (I would argue every time that you **DO NOT** have to do this - ***EVER***) .... – marc_s Apr 10 '17 at 13:47
  • I think that giving some feedback for the user after insertion would be good so it can let the user know whether the row was entered with the value which was shown or whether it was changed. What do you think? – Elham Kohestani Apr 10 '17 at 13:47