473,767 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Auto updating field created from the value of an IDENTITY field

Hi,

I need an auto incrementing field that will contain values like
N000001, N000002, N000003 etc.

I think the way is to use the value from an identity field in a stored
procedure that is triggered at insert.
I can't see that it can be made in pure SQL, but Java is not a problem.

Any of you that can tell me the way of doing it ?
Thanks
Poul
Nov 12 '05 #1
3 3580
Poul Møller Hansen wrote:
Hi,

I need an auto incrementing field that will contain values like
N000001, N000002, N000003 etc.

I think the way is to use the value from an identity field in a stored
procedure that is triggered at insert.
I can't see that it can be made in pure SQL, but Java is not a problem.

Any of you that can tell me the way of doing it ?
Thanks
Poul


You mean like this?

-- This table is used to contain the counters used to generate
-- serial-numbered entries for other tables. The table and column
-- names are in here, as well as the value of the counter. Programs
-- are assumed to increment the value after obtaining it. They
-- should obtain the value and increment it in a single transaction.
CREATE TABLE serial (
serial_table CHAR(30) NOT NULL,
serial_column CHAR(30) NOT NULL,
serial_value INTEGER NOT NULL,
PRIMARY KEY (serial_table, serial_column)
);
-- Initialize the serial table.
INSERT INTO serial (serial_table, serial_column, serial_value)
VALUES ('company', 'company_id', 10001),
('source', 'source_id', 101);

-- This table contains the unique company identifier (company_id) for
-- every company in the database. We would have preferred to use the
-- CRSP PERMNO instead, but we do not know it for every company.
CREATE TABLE company (
company_id INTEGER NOT NULL,
company_name CHAR(40) NOT NULL,
PRIMARY KEY (company_id)
);
CREATE TRIGGER company_insert
AFTER INSERT ON company
FOR EACH ROW MODE DB2SQL
UPDATE serial
SET serial_value = serial_value + 1
WHERE serial_table = 'company'
AND serial_column = 'company_id';

-- This table is used to map the name of the source of data with an
-- integer that will be used in other tables to make them smaller.
CREATE TABLE source (
source_id INTEGER NOT NULL,
source_name CHAR(40) NOT NULL,
PRIMARY KEY (source_id)
);
CREATE TRIGGER source_insert
AFTER INSERT ON source
FOR EACH ROW MODE DB2SQL
UPDATE serial
SET serial_value = serial_value + 1
WHERE serial_table = 'source'
AND serial_column = 'source_id';
--
.~. Jean-David Beyer Registered Linux User 85642.
/V\ Registered Machine 73926.
/( )\ Shrewsbury, New Jersey http://counter.li.org
^^-^^ 3:00pm up 12 days, 3:44, 3 users, load average: 4.07, 4.04, 4.00

Nov 12 '05 #2
Poul,

A sequence offers more flexibility. Apart from being able to convert
to chars, you can apply check digits and other business requirements
for keys. The following is a demo. I've made an assumption about the
maximum value of your key. If it doesn't hold, you can alter the
trigger.

-- create the sequence with a start value matching your digit count.
CREATE SEQUENCE SQ_CHARKEY_BASE
START WITH 1000001
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
CACHE 24;

-- table to demo its use in a trigger.
CREATE TABLE TABLE1 (
TABLE_ID CHAR(10) NOT NULL
,COL1 VARCHAR (30) NOT NULL
);

ALTER TABLE TABLE1 ADD
CONSTRAINT PK_TABLE1 PRIMARY KEY
(
TABLE_ID
)
;

-- Use trigger to automatically assign the key. You can create a UDF
-- if you need the value before inserting.
CREATE TRIGGER BIT1_TABLE1
NO CASCADE BEFORE INSERT
ON TABLE1
REFERENCING NEW AS N
FOR EACH ROW MODE DB2SQL
WHEN (N.TABLE_ID IS NULL)
BEGIN ATOMIC
SET N.TABLE_ID = 'N' || SUBSTR( CAST(NEXTVAL FOR
SQ_CHARKEY_BASE AS CHAR(10)), 2);
END@

-- test it.
INSERT INTO TABLE1 (COL1)
VALUES ('VAL1'),
('VAL2'),
('VAL3');

SELECT *
FROM TABLE1;
Christian.
Nov 12 '05 #3
>
A sequence offers more flexibility. Apart from being able to convert
to chars, you can apply check digits and other business requirements
for keys. The following is a demo. I've made an assumption about the
maximum value of your key. If it doesn't hold, you can alter the
trigger.


Christian, this was exactly what I am looking for.
Thank you very much.

Poul
Nov 12 '05 #4

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

Similar topics

1
2581
by: Ken | last post by:
Need help on the Auto Number or Identity Seed on the Oracle Database I got an Access database that need to be converted to Oracle 9i. Somehow the Trigger we created to simulate the "AUTO NUMBER" on Access could not create the sequence number as soon as the value has been inserted. The sequence number can only be created after we go to the second line. Please see the trigger below. Is there anyway we could create a trigger that could...
11
2792
by: csomberg | last post by:
SQL 2000 I thought I would throw this out there for some feedback from others. I'd like to know if you feel using MS auto-increment field is a good solution these days or should one grow their own ? Thanks, Me.
4
10245
by: Shahar | last post by:
Hi I need to get a field name 'ID'(that is an auto-number field) right after I add a new row to table, it's work like that: myCommand.ExecuteNonQuery(); myCommand.CommandText = "SELECT @@Identity"; // the auto-number fiels int iId = (int)myCommand.ExecuteScalar();
4
7221
by: Mike | last post by:
Hello, I have an autoincrement field and I'm not sure how to use a parameter with it to insert data: this.sqlCommand1.CommandText = "INSERT INTO SickAndTired (String_ID, Input_string) VALUES (@Parameter1,@Parameter2)"; this.sqlCommand1.Connection = this.sqlConnection1;
5
1645
by: Ryan Ternier | last post by:
I'm having an issue with an SQL insert statement. It's a very simple statement, and it's causing too much fuss. strSQL = "INSERT INTO tblFieldLayouts(TypeID, FieldID, OrderID, Hidden) VALUES(" & intTypeID & ", " & intFieldID & ", " & intOrderID & ",0)" comFields.CommandText = strSQL comFields.ExecuteNonQuery()
14
2131
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these grid and write to the database and I set the new Primary Keys and related Fields to the new asigned atuonumbers in the Access.
4
2374
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following problem that has occurred to me. Suppose I have two tables in an SQL Server database. Let's call these tables A and B. Assume that A has two fields: a primary key and another holding a string. In table B there are three fields: a primary key, a foreign key (which links to the primary key in A) and other field holding a string.
3
12013
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when i type in the details. I have just moved over to mysql server with access as the front end. I have setup the sql tables with the customer number as autonumber. When i go into the form and add a new customer it does not generate the
3
3528
by: =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= | last post by:
The problem I'm coming across now is that I can't update such a column, because I have set the auto generated value to true, and LINQ won't let me change such a value, a la "System.InvalidOperationException: Value of member 'Activated' of an object of type 'Customers' changed. A member that is computed or generated by the database cannot be changed." This is fine for identity fields, but the field I'm updating is a bit that starts...
0
9571
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
9405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10013
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
9960
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
9841
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...
1
7383
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5280
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...
2
3533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2807
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.