473,809 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1686
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(i d))) + 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****@sommarsk og.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****@sommarsk og.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****@sommars kog.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****@sommarsk og.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****@sommarsk og.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****@sommarsk og.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 "ORD1000000 1"
then do a left trim to remove the "ORD" from the value then increment
it by one to make a value of "ORD1000000 2". Insert the new row and
commit the transaction.

Jul 23 '05 #10

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

Similar topics

3
2641
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 field and I'm using mysql_insert_id to add fields in other tables subsequently...Well, the auto-increment starts at 21. Here's my question: What would be the method to just at the end (ie. 18-20) "really" delete those "ghost" records or reset...
4
17444
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 vb.net and try to add a record this field is autoincrementing. how can i disable the autoincrement of this field yet serves this as my primary key? thanks in advance
2
6034
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 about this here in the newsgroup.
7
3284
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
1409
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 the data from the database I create a new table with an autoincrement column bind the datagrid to this new table. The auto increment column is displayed as an ID col in the datagrid. The user can add records or modify records at this point, when...
6
16135
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 AutoIncrement Columns to different values but didn't get any errors. Should I be able to set the value of an AutoIncrement Column? I would have thought it couldn't be done as the column value was set when a row was added. -- Dennis in Houston
1
3436
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 1...and adding 1 to every new record. The Autoincrement needs to be separate for each ID. For example when I go to the form and enter a new record in the main form, the SET field should start over at 1 as a default and continue (+1) for each new...
4
2454
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 database, but in this case (log sheets), we have hit 32k log entries over the past 2 years. Is there a simple switch to set the autoincrement from integer to long integer?
8
3078
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 characters long.
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.