Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table with random numbers. Format is first two always 90 (number starts with) and last four can be any combination, like this. And other thing i'd like to add is procedure would check if number already exists or not, if it does skip that number, etc..

EDIT: id like to mention is with this stored procedure, im trying to start from 900001 to end. While doing this itll come to when current number already exists in table, thats when itll skip to next.

What I have tried:

I have tried following this:

Alter procedure Gen_SerialNumber
(
@ItemType char(1),
@ItemBatchNumber varchar(30),
@Date datetime,
@SerialNumber varchar(20) out,
@fifthDigit int
)
AS
Begin 
set @ItemType=(Select ItemType from ItemBatchNumber where ItemBatchNumber=@ItemBatchNumber)
Declare @SerialNumber1 varchar(20)
Set @SerialNumber1='xyz'+''+@ItemType+''+CAST( (Select COUNT(distinct ItemBatchNumber)from ItemBatchNumber
where ItemType=@ItemType) as varchar (10) )
Set @fifthDigit=SUBSTRING(@SerialNumber1,5,1)
if exists(Select SerialNumber from Gen_SN where SerialNumber=null or SerialNumber!=@SerialNumber)
set @fifthDigit=1
if exists(Select mfgDate,ItemBatchNumber from Gen_SN where mfgDate=@Date and ItemBatchNumber=@ItemBatchNumber)
Set @fifthDigit=1
else 
set @fifthDigit=@fifthDigit+1
 
Set @SerialNumber=('xyz'+''+@ItemType+''+cast(@fifthdigit as varchar(2)))
Insert into Gen_SN values(@ItemType,@ItemBatchNumber,@SerialNumber,@Date)
END

Got that from here but unable to figure out since my situation is little different.
Posted
Updated 18-Jul-17 4:43am
v2
Comments
ZurdoDev 18-Jul-17 10:41am    
What is your question?
r00n3y 18-Jul-17 11:06am    
how can i get next sequence number when table doesnt have all in sequence, and when it gets it (make sure its unique).?? See screenshot i posted
ZurdoDev 18-Jul-17 11:11am    
There is no magic way to do it. You just have to write code. Write a function that has a loop and you keep looping until you find the next one that is not used.
r00n3y 18-Jul-17 11:20am    
I know there is no magic way, and writing is where my knowledge lacks.
ZurdoDev 18-Jul-17 11:27am    
Look up how to do a loop in sql and start with that.

1 solution

SQL has a RAND[^] function to create random numbers...

-- UPDATE TO REFLECT OP'S COMMENT
You have 3 options.
1. Random, and that means non-unique (that's RAND)
2. Unique, but not random (sequential) - identity column[^]
3. Both - GUID[^]
 
Share this answer
 
v2
Comments
r00n3y 18-Jul-17 10:44am    
i have looked at it but problem is i cant figure out a way to check if number exists already or not in table
Aria Jafarian 18-Jul-17 10:52am    
Just thinking out loud, Shouldn't you put each generated number in a table and check if it exists or not in that table before using it?
Dave Kreskowiak 18-Jul-17 10:52am    
How about a SELECT for the ID number you generated? Seems kind of obvious.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900