Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Using char as an auto-increment identity field

Author  Topic 

sumo
Starting Member

45 Posts

Posted - 2002-11-14 : 13:38:15
Is it possible to have a field that is a character datatype used as an auto-incrementable identity? The identity data will look like numbers, but they need to be a fixed length (with leading zeros, i.e. 001). We have a very valid reason for wanting to do this and I don't know an easy way to do it without an insert trigger to generate the next #.

Would it be easier to use int or smallint, start the seed at the first # that matches the length I want and use a rule to limit how high it can go (i.e. seed = 100, rule <= 999)? We're going to be using these identities in many different tables to build a large cross-reference table that has all of the id's concatenated together into a character field (i.e. 037858834064).

The explanation as to why we need to do this is rather lengthy to explain here...

Michael

Michael Sumerano
Technical Web Administrator
eKeystone.com
http://www.ekeystone.com/

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-11-14 : 14:45:03
I would say that storing the data in a table and how you use it elsewhere don't always have to be exactly the same.

For example, use an int with a constraint of <999, if 3 digits is your limit, for your primary key. Then, create a VIEW of that table that adds a field which is a char(3) that is your primary key with leading zeroes if needed. Then, you can use that field going forward in other tables.

One other thought: instead of concatenating ID's into one big field, I would suggest a "middleman" table joining ID's from 1 table with ID's from another.

That is, instead of

OtherKey ForeignIDField
1 001002003004005
2 001006008009

You would have:

OtherKey ForeignID
1 001
1 002
1 003
1 004
1 005
2 001
2 006
2 008
2 009

And the primary key of the above table would be (OtherKey, ForeignID).

Good Luck.

BTW -- to pad your ID field into 3 digits:

Right('000' + Convert(varchar,ID),3)



Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-11-15 : 04:41:45
>> We have a very valid reason for wanting to do this.
Doubt that.
But have you looked at computed columns?

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

sumo
Starting Member

45 Posts

Posted - 2002-11-15 : 08:09:54
quote:

>> We have a very valid reason for wanting to do this.
Doubt that.
But have you looked at computed columns?



Actually, there's no need for either. I looked at it a bit more and I can use an integer identity with the seed being the smallest # possible at the length I want. For example, if I want a 3 digit #, I start at 100 and end at 999, 4 digits, 1000 to 9999. It does eliminate usable #'s, but what we're building is a lookup table with a 57 character long string and an ID. The string will contain all of the fixed-length codes in character form from previous selections (it's an automotive catalog) and a generic search can be done on the string using like. It allows us to make our entire catalog available as a web service without defining non-generic calls to the service to generate the search. The less querying the better and we'll be making it fast by creating federated databases and partitioned views...

It's really difficult to explain the complexities of an automotive parts catalog (without a few days to spare). Especially since we're talking about 1.5 million parts in over 650 manufacturers. The catalog we're rebuilding is a vehicle application catalog and we need to give universal access from anywhere...

Michael Sumerano
Technical Web Administrator
eKeystone.com
http://www.ekeystone.com/
Go to Top of Page

anas
Starting Member

1 Post

Posted - 2005-12-13 : 01:44:35
to make your identity field starts from 0001 ,make the field int , seed = 1 and format=0000, so the next will be 0002 etc.

Go to Top of Page
   

- Advertisement -