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. Simple Unique Identity Value

Simple Unique Identity Value

Scheduled Pinned Locked Moved Database
questiondatabasehelp
5 Posts 3 Posters 41 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    I need a way to return an identity value for a SELECT statement that is guaranteed to be unique. So I wrote the following stored procedure:

    CREATE PROCEDURE agsp_UniqueDocnum
    @DOCUMENTTYPE AS NVARCHAR(15),
    @ORDERID AS INT
    AS
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    INSERT INTO ags\_UniqueDocnum 
    (	CreationDate,
    	DocumentType,
    	OrderId )
    VALUES
    (	GETDATE(),
    	@DOCUMENTTYPE,
    	@ORDERID );
    
    
    RETURN SCOPE\_IDENTITY();
    

    END

    The ags_UniqueDocnum table contains an Identity column that I'm using as the unique identifier. But when I try:

    SELECT agsp_UniqueDocnum('TEST', 0)

    It tells me that that's not the name of a FUNCTION. So I'm stuck in a catch 22. You can't use an INSERT inside a function, and you can't SELECT a stored procedure! How can I get a unique identifier within a SELECT statement? It must be all digits, no alpha, and no more than 16 characters in length.

    The difficult we do right away... ...the impossible takes slightly longer.

    Richard DeemingR CHill60C 2 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      I need a way to return an identity value for a SELECT statement that is guaranteed to be unique. So I wrote the following stored procedure:

      CREATE PROCEDURE agsp_UniqueDocnum
      @DOCUMENTTYPE AS NVARCHAR(15),
      @ORDERID AS INT
      AS
      BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

      INSERT INTO ags\_UniqueDocnum 
      (	CreationDate,
      	DocumentType,
      	OrderId )
      VALUES
      (	GETDATE(),
      	@DOCUMENTTYPE,
      	@ORDERID );
      
      
      RETURN SCOPE\_IDENTITY();
      

      END

      The ags_UniqueDocnum table contains an Identity column that I'm using as the unique identifier. But when I try:

      SELECT agsp_UniqueDocnum('TEST', 0)

      It tells me that that's not the name of a FUNCTION. So I'm stuck in a catch 22. You can't use an INSERT inside a function, and you can't SELECT a stored procedure! How can I get a unique identifier within a SELECT statement? It must be all digits, no alpha, and no more than 16 characters in length.

      The difficult we do right away... ...the impossible takes slightly longer.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You'll probably want to look into sequences, which is what SQL Server uses behind the scenes to implement identity columns. Sequence Numbers - SQL Server | Microsoft Learn[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      Richard Andrew x64R 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        I need a way to return an identity value for a SELECT statement that is guaranteed to be unique. So I wrote the following stored procedure:

        CREATE PROCEDURE agsp_UniqueDocnum
        @DOCUMENTTYPE AS NVARCHAR(15),
        @ORDERID AS INT
        AS
        BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;

        INSERT INTO ags\_UniqueDocnum 
        (	CreationDate,
        	DocumentType,
        	OrderId )
        VALUES
        (	GETDATE(),
        	@DOCUMENTTYPE,
        	@ORDERID );
        
        
        RETURN SCOPE\_IDENTITY();
        

        END

        The ags_UniqueDocnum table contains an Identity column that I'm using as the unique identifier. But when I try:

        SELECT agsp_UniqueDocnum('TEST', 0)

        It tells me that that's not the name of a FUNCTION. So I'm stuck in a catch 22. You can't use an INSERT inside a function, and you can't SELECT a stored procedure! How can I get a unique identifier within a SELECT statement? It must be all digits, no alpha, and no more than 16 characters in length.

        The difficult we do right away... ...the impossible takes slightly longer.

        CHill60C Offline
        CHill60C Offline
        CHill60
        wrote on last edited by
        #3

        You can "redirect" the output from the stored procedure into a table variable or temporary table and then select from that (or join the results if the select is meant to be more complex) - a bit of a kludge but works e.g.

        Declare @Temp Table ([Id] [int])
        Insert @Temp Exec agsp_UniqueDocnum 'TEST', 0
        Select * from @Temp;

        Richard Andrew x64R 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          You'll probably want to look into sequences, which is what SQL Server uses behind the scenes to implement identity columns. Sequence Numbers - SQL Server | Microsoft Learn[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          Thank you. I'll check it out.

          The difficult we do right away... ...the impossible takes slightly longer.

          1 Reply Last reply
          0
          • CHill60C CHill60

            You can "redirect" the output from the stored procedure into a table variable or temporary table and then select from that (or join the results if the select is meant to be more complex) - a bit of a kludge but works e.g.

            Declare @Temp Table ([Id] [int])
            Insert @Temp Exec agsp_UniqueDocnum 'TEST', 0
            Select * from @Temp;

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            Thank you.

            The difficult we do right away... ...the impossible takes slightly longer.

            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