Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Is this unique ID generation procedure correct ?

Is this unique ID generation procedure correct ?

Scheduled Pinned Locked Moved Database
questiondatabasebusinessannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    virang_21
    wrote on last edited by
    #1

    After 1000 this will reset counter for specific term to 1 again. Will this stored procedure be 100% reliable to give unique id every time. There are multiple users accessing the web application that may call this stored procedure.

    ALTER PROCEDURE [dbo].[get_next_id](@Term int,@ID INT OUTPUT) AS
    SET NOCOUNT ON
    DECLARE @Current int;
    SELECT @Current=[ID] FROM [IdGenerator] where Term = @Term;
    IF @Current<1000
    BEGIN
    update IdGenerator SET @ID = ID, ID = ID + 1 where Term=@Term
    END
    ELSE
    BEGIN
    update IdGenerator SET @ID =1,ID = 2 where Term=@Term
    END

    One option is to use identity column but that is out of question as to generate unique Id I have to add certain text to each number as that is the part of business requirement. Like add 1001 or 1002 or 1003 etc. in front of numbers. This stored procedure will give me the unique number. Criticize it / Suggest any alternative .

    M D 2 Replies Last reply
    0
    • V virang_21

      After 1000 this will reset counter for specific term to 1 again. Will this stored procedure be 100% reliable to give unique id every time. There are multiple users accessing the web application that may call this stored procedure.

      ALTER PROCEDURE [dbo].[get_next_id](@Term int,@ID INT OUTPUT) AS
      SET NOCOUNT ON
      DECLARE @Current int;
      SELECT @Current=[ID] FROM [IdGenerator] where Term = @Term;
      IF @Current<1000
      BEGIN
      update IdGenerator SET @ID = ID, ID = ID + 1 where Term=@Term
      END
      ELSE
      BEGIN
      update IdGenerator SET @ID =1,ID = 2 where Term=@Term
      END

      One option is to use identity column but that is out of question as to generate unique Id I have to add certain text to each number as that is the part of business requirement. Like add 1001 or 1002 or 1003 etc. in front of numbers. This stored procedure will give me the unique number. Criticize it / Suggest any alternative .

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Criticise - not a problem this is a dumb idea. Record identifiers are just that they identify the record, the business has no input into how they are created, maintained of contain. On the other hand a code used by the users to identify a record is completely up to the business and they can ask for any rules they like Why are you resetting after 1000 and how can that be unique? Suggest - also not a problem Have 2 fields, recordID which is an identity field and record code which store the business defined identifier.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • V virang_21

        After 1000 this will reset counter for specific term to 1 again. Will this stored procedure be 100% reliable to give unique id every time. There are multiple users accessing the web application that may call this stored procedure.

        ALTER PROCEDURE [dbo].[get_next_id](@Term int,@ID INT OUTPUT) AS
        SET NOCOUNT ON
        DECLARE @Current int;
        SELECT @Current=[ID] FROM [IdGenerator] where Term = @Term;
        IF @Current<1000
        BEGIN
        update IdGenerator SET @ID = ID, ID = ID + 1 where Term=@Term
        END
        ELSE
        BEGIN
        update IdGenerator SET @ID =1,ID = 2 where Term=@Term
        END

        One option is to use identity column but that is out of question as to generate unique Id I have to add certain text to each number as that is the part of business requirement. Like add 1001 or 1002 or 1003 etc. in front of numbers. This stored procedure will give me the unique number. Criticize it / Suggest any alternative .

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #3

        Mycroft has given you some good advice. In terms of your original question, yes this will be reliable if you serialize the transactions. Incidentally, this will reset before 1000, not after. I assume that is what you want, so you are generating your business numbers in the range 1001... to 1999...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups