locked
Unique Identifier RRS feed

  • Question

  • Hi all

    I have a table with an Id column which is the primary Key. Is there a script that I could use to find out the next Id number before I insert into the next row.

    All help appreciated as usuall

    Thank you 

    Richard...

    Friday, March 2, 2012 12:28 PM

Answers

  • Is this column an IDENTITY column then you use the IDENT_CURRENT(). 

    http://msdn.microsoft.com/en-us/library/ms175098.aspx 

    if not the you need to issue a select stmt to find the value.

    select max(id) curretValue from yourtable

    VT


    Please mark answered if I've answered your question and vote for it as helpful to help other user's find a solution quicker

    • Marked as answer by RichIEvans Friday, March 2, 2012 12:55 PM
    Friday, March 2, 2012 12:32 PM
  • Hi,

    you cannot find out what id the system would generate next. The problem here is, that the unique identifiers are generated out of multiple factors including the time. So you can ask the database to generate a new id and then insert it but you cannot get the next id that will be calculated at an unknown time later. Please see http://en.wikipedia.org/wiki/Globally_unique_identifier for more details about the unique identifier.

    To get a new id, you can simply call NEWID:
    http://msdn.microsoft.com/en-us/library/ms190348.aspx

    With kind regards,

    Konrad

    • Marked as answer by RichIEvans Friday, March 2, 2012 12:55 PM
    Friday, March 2, 2012 12:37 PM

All replies

  • Is this column an IDENTITY column then you use the IDENT_CURRENT(). 

    http://msdn.microsoft.com/en-us/library/ms175098.aspx 

    if not the you need to issue a select stmt to find the value.

    select max(id) curretValue from yourtable

    VT


    Please mark answered if I've answered your question and vote for it as helpful to help other user's find a solution quicker

    • Marked as answer by RichIEvans Friday, March 2, 2012 12:55 PM
    Friday, March 2, 2012 12:32 PM
  • Hi,

    you cannot find out what id the system would generate next. The problem here is, that the unique identifiers are generated out of multiple factors including the time. So you can ask the database to generate a new id and then insert it but you cannot get the next id that will be calculated at an unknown time later. Please see http://en.wikipedia.org/wiki/Globally_unique_identifier for more details about the unique identifier.

    To get a new id, you can simply call NEWID:
    http://msdn.microsoft.com/en-us/library/ms190348.aspx

    With kind regards,

    Konrad

    • Marked as answer by RichIEvans Friday, March 2, 2012 12:55 PM
    Friday, March 2, 2012 12:37 PM