Posted by: rawbee June 5, 2008
SQL Automatic incremental generator
Login in to Rate this Post:     0       ?        

we can do that several ways...here is one simple idea for getting newid. you can use it as function or can just modify codes to get ur desire result.

 

DECLARE @LastID AS VARCHAR(60) --R000001

DECLARE @NewID AS INT

SET @LastID = (SELECT top 1 SequenceID FROM test ORDER BY SUBSTRING(SequenceID,2,60) DESC) -- Gets LastId in Column

SELECT @NewID =(LTRIM(MAX(SUBSTRING(SequenceID,2,60)+1))) FROM test -- Gets only Numeric Part and Increment by 1

SELECT REPLACE(@LastID,RIGHT(@LastID,LEN(@NewID)),@NewID)AS NextID -- Replace NewId

 

Let me know if you have question on my query.

Read Full Discussion Thread for this article