Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I want to create a auto generate unique Id which contains today's date, month, year and some number.

Ex. in year 2016-only 16 will be used,
for the month of January 01 will be used,
for today's day 26 would be used,
and the slno is starts 0001 to 9999

So my id should be like 1601250001.

The date changes everyday and number should be increases ..


I Have tried ..

SQL
Create procedure InsertRegistration
(
    @name nvarchar(50),
    @Address nvarchar(100)
)
as
begin
    DECLARE @BdID VARCHAR(25)

    SET @BdID = (SELECT convert(varchar, getdate(), 112))+
                 CAST(((SELECT COUNT(*) 
                     FROM BasicInformation  
                 WHERE @BdID like (SELECT convert(varchar, getdate(),112))+'%') 
                       +1 ) AS VARCHAR(5))

     Insert into BasicInformation(BdID, name, address) 
     values (@BdID, @name, @address)
end

It's working but the increment is not happening for each registration. I am a beginner. Please help me
Posted
Updated 25-Jan-16 20:14pm
v2

1 solution

A moderate level design could be like

* Have a table COUNTERS(ID, currentValue, maskingRules)
* With values (1, 1, 'yymmdd9999')
* Have a SQL Function which does following
* Read masking rule
* Parse it and fill it accordingly
* Update CurrentValue
* Return the accumulated value to the business layer

This will help in following ways
* You have an independent function that can be easily tested
* You can define different masking rules
* You can change the rules instantly (on production)

This approach do have side effects, significant one is the COUNTERS table could it self be a bottleneck on load because of locks if too many counters have been defined
 
Share this answer
 

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