473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is there a way to autoincrement a field with mixture of alpahbetic characters and numbers?

I have a feeling I'll be forced to use a script and a trigger for this
type of field format but I'm wondering if any of your wizards could
point at a simple way I could do something like this:

For example, if I want to be able to keep track of new orders following
this incrementing convention:

ORD100000001
ORD100000002
ORD100000003
.... etc ...

Does MSSQL2000 have features that I can simply set for this kind of
field or will I be resorting to writing up a SQL script and a trigger?

Jul 23 '05 #1
11 1656
Jared Evans (jn*****@gmail.com) writes:
I have a feeling I'll be forced to use a script and a trigger for this
type of field format but I'm wondering if any of your wizards could
point at a simple way I could do something like this:

For example, if I want to be able to keep track of new orders following
this incrementing convention:

ORD100000001
ORD100000002
ORD100000003
... etc ...

Does MSSQL2000 have features that I can simply set for this kind of
field or will I be resorting to writing up a SQL script and a trigger?


You could define this textual order ID as computed column:

textid = 'ORD1' + space(8 - len(ltrim(str(id))) + ltrim(str(id))

id is here you numeric id column. This could be an IDENTITY column,
or one that you increment yourself. Notice that if you have a requirement
for ids being contiguous, you cannot use IDENTITY.

This examples presumes that ORD1 is a fixed string. If ORD1 is variable,
you might still be able to use a computed column if you compute ORD1
with a UDF. However, for the sake of performance, a trigger would be a
lot better in this case.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
This table will also have other rows such as

INV90000001
INV90000002
INV90000003
PYMT4000001
PYMT4000002

etc...

For example, the next PYMT row would be
PYMT4000003

so if I insert a row that is of type "Payment", it would autoincrement
to this next value in the sequence.

Since I would much prefer to keep tracking this kind of sequence, I
would not be able to use the id column in this case.

Jared

Jul 23 '05 #3
Jared Evans (jn*****@gmail.com) writes:
This table will also have other rows such as

INV90000001
INV90000002
INV90000003
PYMT4000001
PYMT4000002

etc...

For example, the next PYMT row would be
PYMT4000003

so if I insert a row that is of type "Payment", it would autoincrement
to this next value in the sequence.

Since I would much prefer to keep tracking this kind of sequence, I
would not be able to use the id column in this case.


You could have two columns, one with the type and one with the id. Or
maybe even three, as that digit after the code is something you have
not explained yet. And then you could have an computed column that composes
the strings. If you will use the string for lookups, you will need to
index the column. The one thing to keep in mind is to make sure that
you will have a couple of settings on, as described in Books Online.
Of these, the trickeist is ARITHABORT, since it is not ON by default.

As for the id, you would have roll your own, but that's simple:

BEGIN TRANSACTION
SELECT @id = coalesce(MAX(id), 0) + 1 FROM tbl (UPDLOCK)
WHERE type = @type

INSERT tbl (id, type, ...
VALUES (@id, @type, ...

...
COMMIT TRANSACTION
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
I agree, I posted a similar response to the original post B4 I read this
reponse.
"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn**********************@127.0.0.1...
Jared Evans (jn*****@gmail.com) writes:
This table will also have other rows such as

INV90000001
INV90000002
INV90000003
PYMT4000001
PYMT4000002

etc...

For example, the next PYMT row would be
PYMT4000003

so if I insert a row that is of type "Payment", it would autoincrement
to this next value in the sequence.

Since I would much prefer to keep tracking this kind of sequence, I
would not be able to use the id column in this case.
You could have two columns, one with the type and one with the id. Or
maybe even three, as that digit after the code is something you have
not explained yet. And then you could have an computed column that

composes the strings. If you will use the string for lookups, you will need to
index the column. The one thing to keep in mind is to make sure that
you will have a couple of settings on, as described in Books Online.
Of these, the trickeist is ARITHABORT, since it is not ON by default.

As for the id, you would have roll your own, but that's simple:

BEGIN TRANSACTION
SELECT @id = coalesce(MAX(id), 0) + 1 FROM tbl (UPDLOCK)
WHERE type = @type

INSERT tbl (id, type, ...
VALUES (@id, @type, ...

...
COMMIT TRANSACTION
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 23 '05 #5
I will be pursing this solution:

I'll keep separate tables for ORD, INV, PYMT, etc.

in each of these tables (the ORD table for example)

Type, ID, TrxNum (the composite of previous 2 fields), etc
ORD, 10000001, ORD1000001, etc

ID would be an identity that increments by one.
Insert a row into this table first, grab the identity and create and
update(or calculated field) the TrxNum field. Take the TrxNum field
to insert into other tables and use as link between the tables as the
relationship.

Jul 23 '05 #6
[posted and mailed, please reply in news]

Jared Evans (jn*****@gmail.com) writes:
I will be pursing this solution:

I'll keep separate tables for ORD, INV, PYMT, etc.

in each of these tables (the ORD table for example)

Type, ID, TrxNum (the composite of previous 2 fields), etc
ORD, 10000001, ORD1000001, etc

ID would be an identity that increments by one.
Insert a row into this table first, grab the identity and create and
update(or calculated field) the TrxNum field. Take the TrxNum field
to insert into other tables and use as link between the tables as the
relationship.


I ask you again: can you accept sequences like:

ORD1000001
ORD1000002
ORD1000004
ORD1000008

That is, can you accept gaps? If you cannot, do *not* use IDENTITY.

Whether you should have separate tables at all, I can't tell, because
I don't know the business and data model behind it. But if ORD100001
and PYMT400001 ends up the same table, you should not have separate
tables.

Particularly, if a new code could be added tomorrow, it's a very poor
choice to have separate tables. If you have all in one table, is a
matter of adding that new code to the table. If you have one table
each, you will need to recode each time.

Having separate tables only get access to the IDENTITY function is
poor design, in my opinion.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7
>From further email discussions with Erland, using IDENTITY will not
reliably increment the field by one. I don't think I read that line
close enough:

"Notice that if you have a requirement for ids being contiguous, you
cannot use IDENTITY. " I apologize for keeping pushing the IDENTITY
issue to keep track of the sequence when you made it clear that it
would not work the way I thought it would.

I think I will be able to drop the project requirement for a contiguous
sequence and defer to using the id field even if it may not always be
incremented by one. That would reduce the load on the database and
help it to scale better. I'm not sure if the benefit of using the next
number in sequence would outweigh the performance penalty that would be
incurred.

Jul 23 '05 #8
Jared Evans (jn*****@gmail.com) writes:
I think I will be able to drop the project requirement for a contiguous
sequence and defer to using the id field even if it may not always be
incremented by one. That would reduce the load on the database and
help it to scale better. I'm not sure if the benefit of using the next
number in sequence would outweigh the performance penalty that would be
incurred.


The load as such for a rolling-your-own solution is about neglible. The
case where you get problem is if the INSERT operation is part of a
longer transaction. In this case, another attempt to insert can be
blocked. If the number of inserts is high, you can get severe contention
problems.

On the oher hand, in our system we have plenty of tables where we don't
use IDENTITY, including transactional ones. (We do have business
requirements that some series must be sequential.) For the normal flow,
this does not seem to cause much problems. Only where there is a batch
operation, that runs for several minutes, you can see problems.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #9
What would you recommend as a solution to keep the sequence contiguous?
Could you give an example of a such T-SQL function?

I probably would start a transaction to take the value "ORD10000001"
then do a left trim to remove the "ORD" from the value then increment
it by one to make a value of "ORD10000002". Insert the new row and
commit the transaction.

Jul 23 '05 #10
Jared Evans (jn*****@gmail.com) writes:
What would you recommend as a solution to keep the sequence contiguous?
Could you give an example of a such T-SQL function?


OK, I'm rewinding the thread, and repost this:

BEGIN TRANSACTION
SELECT @id = coalesce(MAX(id), 0) + 1 FROM tbl (UPDLOCK)
WHERE type = @type

INSERT tbl (id, type, ...
VALUES (@id, @type, ...

...
COMMIT TRANSACTION
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #11
ok, now it all makes sense to me.

much thanks!

Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: leegold2 | last post by:
Say I got 20 records in a MYSQL DB. I go into PHPMyAdmin and drop (it's that "X" icon) the last three (3) records. Then I run a PHP script to add 3 records to the DB. I have an autoincrement PK...
4
by: jaYPee | last post by:
I have a table in my sqlserver 2000 that has a field IDNO. i want this field to be my primary key. however i don't want this field to use the autoincrement feature. when i access this table from...
2
by: Paulo Rodrigues | last post by:
Hi I would like some help about the following : I have a text field and I don't want it contains any numbers. How can I limit this situation ? So far, I couldn't find literature exacly...
7
by: Amy | last post by:
I'm trying to add an autoincrementing id to a table based on an existing field Name, but Name has duplicated records. How can I do that in ACCESS? Thanks. Amy
0
by: Neil | last post by:
Hi, I'm getting some strange results using the autoincrement column on my datatable. I'm populating a datatable with data from my database and displaying this in a datagrid. The first time I get...
6
by: Dennis | last post by:
I have set a DataTable and one of the columns I set "AutoIncrement" to True. I then populate the Table by setting the columns to values then add the row to the table. I inadverently set the...
1
by: Mike | last post by:
I have a form that has an embedded subform (Datasheet View) that are linked based on a 1-many ID field. I have a field (SET) in my embedded subform that I want to Autoincrement starting with...
4
by: Tim | last post by:
Hello All, I could use some help on an error that is just now popping it's head up. Seems that the autoincrement numeric has hit 32,767. The autoincrement is used in various locations in the...
8
by: jumperbl | last post by:
Hi, I am a noob and I was wondering if there is way to generate random mixture of alphabets and numbers? For example: 1234567890ABCDEFG or 4BA92ABD293BC3890 the length needs to be 17...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.