Click here to Skip to main content
15,887,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
i am using sql server 2005 and C#.net, i have a table name patient the primary key of this table is (LAB_Id int) which auto increment is true and seed in one the value of this column is like 11000001,11000002,11000003.........and so on now i want that when the year change from 2011 to 2012 my LAB_Id automatically be start from 0 but like this 12000001, 12000002, 12000003.............so on and in 2013 it should be start like 13000001, 13000002,13000003............. this means that first two digit represent the year and with every year changing it should be automatically change.
is it possible if so help me with the help of code
Posted

Remove the identity spec for the column & calculate & insert the value yourself

You can get the highest Id using
Select MAX(Lab_Id) from patient

You can increment & insert the new value

If the year has changed, calculate the new id like
int Id = ((DateTime.Today.Year % 100) * 1000000) + 1


============================================

Alternatively, you can still use auto increment & choose to temporarily turn off auto increment and insert value when the year has changed.

You can use
SET IDENTITY_INSERT patient ON

After this, you can insert custom id value and turn off using
SET IDENTITY_INSERT patient OFF


Remember that IDENTITY_INSERT can be turned on for only one table at a time.
 
Share this answer
 
Comments
sher ali 11-May-11 4:51am    
is it possible to by triger to create a triger and when the year chang the triger fire auto
and the triger is like this
mytriger
{
if(lab_id identity seed!=(DateTime.Today.Year % 100) * 1000000))
lab_id identity seed=(DateTime.Today.Year % 100) * 1000000)
}
if so plz write the triger for me because i never used triger i am new with triger
strogg 11-May-11 15:46pm    
I suppose you can. Writing a trigger is the same. If you have the management studio just go ahead, create an insert trigger on the table & put the code there. But it may mess up the values returned by the Select @@IDENTITY statements (if you're using them). You'll have to try. Even i don't use triggers.
I generally avoid putting anything that looks like meaningful data in a primary key / ID field. It's also generally preferable to have separate fields for each item of data (rather than any form of composite) and then add an ID field whose sole purpose is to uniquely identify each record (primary key and field used to link records between tables in 1-many relationships - and can be any unique gobbledegook you like as it can be automatically generated and, apart from debugging etc., is never seen by a human).

In your case I'd have:
Field 1 - ID - autoincrement integer or (my preference) a Guid
Field 2 - Year
Field 3 - Patient no.

Just a thought . . .
 
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