开发者

how to increment the id in a table starting from 001

开发者 https://www.devze.com 2023-01-23 14:59 出处:网络
I have taken a registered table... and for each registered detail there must be a specific id to be created in another table... which will start from and the id must start from 001 , how to do? i have

I have taken a registered table... and for each registered detail there must be a specific id to be created in another table... which will start from and the id must start from 001 , how to do? i have taken开发者_如何转开发 sql server 2005 database


You would have to use a varchar and add padded zeros to the left. This is bad-practice really, you should be using an identity column which is an integer.

If you want to display your id like this to the user, then have it stored as an int (1, 2, 3... 47) in the database, but then when presenting it have some code which handles it then:

PSEUDO CODE:

if (id < 100)
 return "0" + id;
else if (id < 10)
 return "00" + id;
else
 return id;


use a computed column that does what beardtwizzle proposes!

http://msdn.microsoft.com/en-us/library/ms191250.aspx

Remember to persist it for performance!

0

精彩评论

暂无评论...
验证码 换一张
取 消