473,657 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to reset/restart/modify the IDENTITY counter?

Hi!

I guess this must be a FAQ but I'll give it a try.
I've searched the web and usenet archive and found only negative
answers. But most of them were dated like 3-4 years back. Hasn't
anything changed since then? DB2 V7.2.
The problem:
I have a table with a primary key GENERATED BY DEFAULT AS IDENTITY.
Then I INSERT several rows and force their PK's value. Then I see that
my INSERTs haven't modified the value of DB2's internal IDENTITY
counter so subsequent INSERTs (this time with generated PK value) will
eventually conflict with existing rows resulting in 803 error.
So how can I load my table (from a script) with some initial data
before I launch my system?

Best regards

WhoReallyCares

Nov 12 '05 #1
6 74171
klh
> So how can I load my table (from a script) with some initial data
before I launch my system?


If this DB2 on NT after you load the table run an SQL command to find
the maximum key value.

select max(key_value) from tblname

Then alter the table like:
alter table tblnm alter column key_nm restart with max_key_value + 1

Where max_key_value + 1 is whatever your previous select statement
showed.

If this is DB2 on os/390 or z/0S then create the table initially with
the generated ID starting at a value higher than the highest value
you'll be loading. Then load the table.

HTH,
klh

Nov 12 '05 #2
Hi!

While browsing the internet I've found an outline of a solution to
simulate "ALTER TABLE ... RESTART WITH ..." on DB2 v7. It went like
this:

1. Instead of having IDENTITY column in your table, create another
table (counter) with a single column and define it as IDENTITY
2. Create a trigger in your table and have it: a) insert a record into
"counter" table, b) obtain the IDENTITY value, c) use this value in
your table, d) delete the record from the "counter" table
This is transparent to the application and the advantage is that you
can always drop and re-create the "counter" table with required initial
value of its IDENTITY counter.

So this is what I did:

1.
Instead of having normal table with IDENTITY column:

CREATE TABLE TAB (
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY CONSTRAINT PK
PRIMARY KEY,
PAYLOAD INTEGER)

I had two tables:

CREATE TABLE TAB (
ID INTEGER WITH DEFAULT NOT NULL CONSTRAINT PK PRIMARY KEY,
PAYLOAD INTEGER)
CREATE TABLE CNTR (
ID INTEGER GENERATED ALWAYS AS IDENTITY)

2.
Trigger:

CREATE TRIGGER TRG
NO CASCADE BEFORE
INSERT ON TAB
REFERENCING NEW AS NEW
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
INSERT INTO CNTR VALUES (DEFAULT);
DELETE FROM CNTR;
UPDATE TAB
SET ID = IDENTITY_VAL_LO CAL();
END

Unfortunately this doesn't work because INSERTs are illegal in BEFORE
triggers. Rats! Why???

I could imagine that I can't do INSERTs to the same table but why not
some other table??? DB2 sucks! :-)

Ok, so another try:

CREATE TRIGGER TRG1
NO CASCADE BEFORE
INSERT ON TAB
REFERENCING NEW AS NEW
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
SET NEW.ID = -1; --Mark the record somehow
END
CREATE TRIGGER TRG2
AFTER
INSERT ON TAB
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
INSERT INTO CNTR VALUES (DEFAULT);
DELETE FROM CNTR;
UPDATE TAB
SET ID = IDENTITY_VAL_LO CAL()
WHERE ID = -1; --Find the marked record
END

Now it's starting to be really ugly and clumsy but works:

INSERT INTO TAB (PAYLOAD) VALUES (100)

SELECT * FROM TAB

ID PAYLOAD
-- -------
1 100

Unfortunately the application can't obtain the IDENTITY value of the
inserted record:

SELECT IDENTITY_VAL_LO CAL() FROM SYSIBM.SYSDUMMY 1 yields NULL.

I found the explanation in the documentation of IDENTITY_VAL_LO CAL():
"The INSERT statement must also be issued at the same level; that is,
the value must available locally at the level it was assigned, until it
is replaced by the next assigned value. (A new level is initiated each
time a trigger or routine is invoked.)"

So I tried in vain.

Now the question: How do you think, did the original inventor of the
solution mentioned in the beginning of my post really had some working
solution or only THOUGHT they had a solution. (This resembles the known
case with Fermat's last theorem haha).

Is there any way to cross those "levels" and pass the IDENTITY value
from within my trigger to the application???

I'll appreciate any comments. Thanks.

WhoReallyCares

P.S.
By the way. If I post this from Google Gropus, will it appear on Usenet
servers worldwide??

Nov 12 '05 #3
*sigh* This works on DB2 V7.2 for LUW which is what
you're running right? If you are on DB2 V7.1 for LUW it's about time you
catch up with 3 years worth of fixpack....

SET SCHEMA = MYSCHEMA
$

DROP PROCEDURE GOODSTUFF.SYNCI DENTITY
$
DROP TABLE T
$

CREATE PROCEDURE GOODSTUFF.SYNCI DENTITY(IN schemaname VARCHAR(128),
IN tablename VARCHAR(128))
BEGIN
DECLARE sqlcode INTEGER;
DECLARE maxid BIGINT;
DECLARE idcolname VARCHAR(128);
DECLARE stmttxt VARCHAR(1000);
DECLARE s STATEMENT;
DECLARE cur CURSOR FOR s;

SELECT colname INTO idcolname
FROM SYSCAT.COLUMNS
WHERE tabname = tablename
AND tabschema = schemaname
AND identity = 'Y';
IF SQLCODE = 100 THEN
SIGNAL SQLSTATE '78000'
SET MESSAGE_TEXT = 'can''t find identity column';
END IF;
SET stmttxt = 'SELECT MAX("' || idcolname || '") FROM "' ||
schemaname || '"."' || tablename || '"';
PREPARE s FROM stmttxt;
SET maxid = 0;
OPEN cur;
FETCH cur INTO maxid;
CLOSE cur;
SET stmttxt = 'ALTER TABLE "' || schemaname || '"."' || tablename ||
'" ALTER COLUMN "' || idcolname ||
'" RESTART WITH ' || CHAR(maxid + 1);
EXECUTE IMMEDIATE stmttxt;
END
$

SET PATH = PATH, GOODSTUFF
$

CREATE TABLE T(pk INT NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
c1 INT)
$
INSERT INTO T(c1) VALUES 1, 2, 3,4 ,5, 6
$

SELECT * FROM T ORDER BY pk
$

INSERT INTO T(pk, c1) VALUES (12, 7), (13, 8)
$

CALL SYNCIDENTITY('M YSCHEMA', 'T')
$

INSERT INTO T(c1) VALUES 9, 10, 11
$

SELECT * FROM T ORDER BY pk
$

PK C1
----------- -----------
1 1
2 2
3 3
4 4
5 5
6 6
12 7
13 8
14 9
15 10
16 11

11 record(s) selected.
Cheers
Serge

PS: Makes me think just how much money I could make as a consultant
instead of a salary-man

PPS: Shall I submit a developerWorks DB2 article on this?

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4
We're not worthy, we're not worthy :-)

Seriously, if you began charging for the frequent and high quality
advice you provide here, we (the customers) and IBM would be deprived
of a very valuable service. Besides, I couldn't possibly afford you
even WITH the US/Canadian exchange rate.

Pete H

Serge Rielau wrote:
*sigh* This works on DB2 V7.2 for LUW which is what
you're running right? If you are on DB2 V7.1 for LUW it's about time you catch up with 3 years worth of fixpack....

SET SCHEMA = MYSCHEMA
$
<more excellent code snipped>
Cheers
Serge

PS: Makes me think just how much money I could make as a consultant
instead of a salary-man

PPS: Shall I submit a developerWorks DB2 article on this?

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab


Nov 12 '05 #5
Serge Rielau wrote:
*sigh* This works on DB2 V7.2 for LUW which is what
you're running right? If you are on DB2 V7.1 for LUW it's about time you catch up with 3 years worth of fixpack....
I'm running DB2 V7.1 for z/OS. (in original post I wrote 7.2 but it
proved to be a mistake).
PS: Makes me think just how much money I could make as a consultant
instead of a salary-man

I'm not sure what you're trying to say. Your smart procedure uses
"ALTER TABLE ... ALTER COLUMN ... RESTART WITH ..." syntax, which is
not available to me. So your reply doesn't fit to my problem, I guess.
What I was trying to achieve was to simulate "RESTART WITH"
functionality.

WhoReallyCares

Nov 12 '05 #6
Wh************* ********@gmail. com wrote:
Serge Rielau wrote:
*sigh* This works on DB2 V7.2 for LUW which is what
you're running right? If you are on DB2 V7.1 for LUW it's about time


you
catch up with 3 years worth of fixpack....

I'm running DB2 V7.1 for z/OS. (in original post I wrote 7.2 but it
proved to be a mistake).

PS: Makes me think just how much money I could make as a consultant
instead of a salary-man


I'm not sure what you're trying to say. Your smart procedure uses
"ALTER TABLE ... ALTER COLUMN ... RESTART WITH ..." syntax, which is
not available to me. So your reply doesn't fit to my problem, I guess.
What I was trying to achieve was to simulate "RESTART WITH"
functionality.

WhoReallyCares

Ah.. now that starts to make sense.
I just checked DB2 V8 for zOS and RETART is supported.
You may want to:
* bug your boss when you can get DB2 V8
* bug your IBM Rep and ask whether the feature can be made abvailable
on DB2 V7. I can't imagine you being the first to need it.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #7

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

Similar topics

11
3190
by: Konrad Den Ende | last post by:
I have a function returning a string but the problem is that the color of it is blue which suits me well for some pages but not for others. Is it possible to "feel" what the color of the background in the current document is and set the color of the output accordingly? The background will be an image, in most cases. -- Kindly Konrad
2
1798
by: Yaro | last post by:
Hi UDB 8.2.2 Win I am little confused about IDENTITY behavior. create table aaa( c1 integer not null generated by default as identity, c2 integer, primary key (c1)
3
2606
by: deja | last post by:
Hi, In IIS6, is there a way to reset an individual web application rather than the whole IIS? Does that happen when you change something in Web.config and save it? Thanks, Chumma Dede
4
7280
by: shorti | last post by:
Can anyone explain in greater (and more comprehensive) detail what the RESTART option does in the ALTER TABLE table ALTER COLUMN statement. This is the description in Info Center: RESTART or RESTART WITH numeric-constant Resets the state of the sequence associated with the identity column. If WITH numeric-constant is not specified, the sequence for the identity column is restarted at the value that was specified, either implicitly or...
5
9004
by: Jay | last post by:
In a sub procedure how do I recall/restart the sub and maintain the position in a loop? Eg (this doesnt work how I need it to work)... sub Get_Language() 'code here to prompt the user to do something '...
3
10124
blackstormdragon
by: blackstormdragon | last post by:
Here were our instructions: "My mother always took a little red counter to the grocery store. The counter was used to keep tally of the amount of money she would have spent so far on that visit to the store if she bought everything in the basket. The counter had a four-digit display, increment buttons for each digit, and a reset button. An overflow indicator came up red if more money was entered than the $99.99 it would register. (This was a...
11
3773
by: Jon Slaughter | last post by:
Is there any way to start a terminated thread without using a pool or creating a new thread object? void counter() { clicks = 0; clock.Start(); while (counterActive) { clicks++;
0
4016
by: Frank Swarbrick | last post by:
>>On 6/10/2008 at 4:30 PM, in message <ebf88f96-d8b0-4dfc-85ed-a3a5c44ae4dd@i18g2000prn.googlegroups.com>, <anuritab@gmail.comwrote: Seems like that would work, unless you are actually using a SEQUENCE and not an IDENTITY column. There is also this option, if it's an IDENTITY column: ALTER TABLE my_table ALTER COLUMN my_id RESTART WITH 12345;
0
927
by: robin | last post by:
PROBLEM I need to know how to reset the ink level counter of the Canon MP160. It says " Low ink" when I just refilled the ink tanks. Please help. I need the tricks or software that resets the counter. find solution here http://pc-solution4u.blogspot.com/search/label/Canon%20PIXMA%20MP160%20All-In-One%20InkJet%20Printer
0
8310
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
8826
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
8732
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
8503
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
8605
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
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1615
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.