Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
Sir/Madam
I have a Web Application in Asp.Net With My Sql Server 2008 .

Question: I want to Generate Unique Id Like Current Year Current Month Current Date With 0000 -9999 Number. Example 201310260001
it should be autoincrement .. any solution will be appreciate and this will be done with Store Procedure. When user submit form then that procedure will check the last id and increment with one 201310260002



Thanks In Advance
Posted
Comments
Mike Meinz 26-Oct-13 12:05pm    
Two options

1. Use IDENTITY(1,1) on the primary key column in the table that will store the data? See Scope_Identity in SQL Server documentation.

2. Use SQL Server Sequence Class if your version of SQL Server supports it.

Also you can use unique identifier ,but be noted this is not optimize ever! as it creates a unique value in all the world so it is larg!
SQL
CREATE TABLE myTable (ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID()) 

and for insertion:
SQL
insert into myTable(ColumnA) values(NEWID())

for more information check the links:
http://technet.microsoft.com/en-sg/library/ms189786.aspx[^]
http://technet.microsoft.com/en-sg/library/ms190348.aspx[^]
 
Share this answer
 
i guess the following code will fulfill your requirement. It will generate and increase id based on date and store it to table named #MyTable. Please check and test the code. I tested and found ok with your requirement

SQL
create table #MyTable(Id bigint, Name varchar(100));
go

create proc [dbo].[InsertMaxValue]
as
begin
    
	declare @IdGenByCurrentDate bigint = (select cast(year(getdate()) as varchar) + cast(month(getdate()) as varchar) + + cast(day(getdate()) as varchar));
	declare @LastId bigint = (select max(id) from #MyTable);
	declare @NewId bigint = cast(@IdGenByCurrentDate as varchar(100)) + '0001';

	if @LastId is not null	
	begin
		declare @LastIdGenByCurrentDate bigint = substring(cast(@LastId as varchar(100)), 1, len(@IdGenByCurrentDate));

		if @IdGenByCurrentDate = @LastIdGenByCurrentDate		
			set @NewId = @LastId + 1;			
	end
	insert into #Mytable(Id,Name) values (@NewId, cast(getdate() as varchar(111)));
end

exec InsertMaxValue

select * from #MyTable
 
Share this answer
 
v2
Comments
Mike Meinz 27-Oct-13 8:16am    
What if two application instances call this stored procedure at exactly the same time? Both will select the same MAX(id) and then generate a duplicate NewId. A rare event but it could happen in this age of multiple processors. I prefer to use SQL Server's Built-In functionality to guarantee a unique integer.
S. M. Ahasan Habib 27-Oct-13 11:40am    
I agree that auto increment number should be generated from database either auto identity or sequence number or auto generated guid. Otherwise concurrency issue might be there. Here just i implement the solution that ask for(based on datetime and new number sequence).
Mohammad Nawaz 10-Nov-13 22:43pm    
Thanks all for valuable reply's i made it

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