473,511 Members | 14,393 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 3565
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
2560
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"...
11
2767
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...
4
10221
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...
4
7193
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...
5
1633
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("...
14
2104
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...
4
2361
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...
3
11976
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...
3
3512
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...
0
7251
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
7367
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,...
0
7430
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...
0
5673
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,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
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...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
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...

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.