How to create an auto generated( identity) column?

Posted by Kk86kiran under Sql Server on 4/16/2012 | Points: 10 | Views : 2946 | Status : [Member] | Replies : 3
Hi,

I want to generate an auto generated column value with prefix.

For example i have a class table with cid,clsname. For this table i want to generate cid automatically with cls prefix.

cid clsname

cls1 1st
cls2 2nd


like this i want table

k.m.j.kiran


Responses

Posted by: Rapartipoorna on: 4/16/2012 [Member] Starter | Points: 25

Up
0
Down
sqlconnection con=new sqlconnection("connection string here");
sqlcommand cmd=new sqlcommand("select max(cid) from table);//here we are getting max value of id from table;
datareader dr=cmd.executereader();
if(dr.read)
{
string x= dr[0].tostring();
int y= convert.toint32(x.substring(0,3));here we are getting substring that is like 1,2,3 depends on max value;
y++;//here we are incrementing substring value...

}

else

{
id=cls1;
}


this will help for u...

just see this and implement..

Kk86kiran, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 6/19/2012 [Member] Bronze | Points: 25

Up
0
Down
Please try this query

create table sample12(sid int identity(101,1) primary key not null,sname varchar(30))

Kk86kiran, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response