473,394 Members | 1,951 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,394 software developers and data experts.

UDb V8.FP6 - Finding NEXTVAL for IDENTITY COLUMN (not SEQUENCE)

Hi:

we have column with GENERATED ALWAYS AS DEFAULT. So, we can insert into
this column manually and also let db2 generate a value for this column.

Given a scenario, how can i find the NEXTVAL for this identity column?

We can do "VALUED NEXTVAL FOR SEQUENCE", but for identity how can it be
done?

Thanks.

Vijay

Nov 12 '05 #1
4 9531
UDBDBA wrote:
we have column with GENERATED ALWAYS AS DEFAULT. So, we can insert into
this column manually and also let db2 generate a value for this column.

Given a scenario, how can i find the NEXTVAL for this identity column?

We can do "VALUED NEXTVAL FOR SEQUENCE", but for identity how can it be
done?

Well, when you do NEXT VALUE FOR s1 you are more than doing a peek, you
actually consume the value. So the nextvalue will be a value after that ...
The only way I can think of to get similar function is this:
INSERT INTO T(id) VALUES(DEFAULT);
ROLLBACK;
VALUES identity_val_local();

or (FP4 and higher):
SELECT id INTO :myid FROM NEW TABLE(INSERT INTO T(id) VALUES(DEFAULT));
ROLLBACK;

If all you want to know is where the high water mark is (e.g. to then do
a RESTART) take a look at the proc I posted not too long ago here:

CREATE PROCEDURE GOODSTUFF.SYNCIDENTITY(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;
COMMIT;
END
$
If you want to know what the system will start assigning soon (I.e. the
last cached value) you need SYSIBM.SYSSEQUENCES.LASTASSIGNEDVAL
The follwing view may lead you how to correlate the table to the sequence.
create view syscat.colidentattributes (tabschema, tabname, colname,
start,
increment, minvalue, maxvalue, cycle, cache, seqid) as select
c.tbc
reator, c.tbname, c.name, s.start, s.increment, s.minvalue,
s.maxvalue, s.
cycle, s.cache, s.seqid from sysibm.syscolumns as c,
sysibm.sysdependenci
es as d, sysibm.syssequences as s where c.tbcreator = d.dschema and
c.tbname = d.dname and d.bname = s.seqname and
d.bschema = s.seq
schema and c.identity = 'Y' and d.dtype = 'T' and
d.btype =
'Q' and s.seqtype = 'I'
Note that the SYSIBM.* tables are not docuemented. Their definitions may
change on release boundaries.

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

Well,

just to know the value after inserting I think IBM introduced the
Select over Insert, Update, Delete
statement in V8 (at least 8.2) if I remember correctly.

Then you can use the inserted value for other purposes as well.
This avoids using a sequence just because you need to know which value your
ID has to insert it somewhere else as well. Furthermore, even with sequences
it avoids some locking.

Juliane

UDBDBA wrote:
Hi:

we have column with GENERATED ALWAYS AS DEFAULT. So, we can insert into
this column manually and also let db2 generate a value for this column.

Given a scenario, how can i find the NEXTVAL for this identity column?

We can do "VALUED NEXTVAL FOR SEQUENCE", but for identity how can it be
done?

Thanks.

Vijay

--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200507/1
Nov 12 '05 #3
Hi Serge:

With CACHE = 20, if we query SYSIBM.SYSSEQUENCES, it will show 20 till
you cross 20, then it would show 40.

So, i cannot use this column to find the next value to assign for the
identity column.

The column name is misleading, instead of LASTASSIGNEDVAL it should be
LASTCACHEDVAL...

So, to get the functionality CACHE should be turned OFF.

Vijay

Nov 12 '05 #4
UDBDBA wrote:
Hi Serge:

With CACHE = 20, if we query SYSIBM.SYSSEQUENCES, it will show 20 till
you cross 20, then it would show 40.

So, i cannot use this column to find the next value to assign for the
identity column.

The column name is misleading, instead of LASTASSIGNEDVAL it should be
LASTCACHEDVAL...

So, to get the functionality CACHE should be turned OFF.

Vijay

Vijay,

Have you considered switching to using a sequence instead?
A simple BEFORE INSERT TRIGGER will preserve the semantics for your apps.

Cheers
Serge

PS: You'd have to watch out for LOAD since load doesn't fire triggers.

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

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

Similar topics

4
by: Aaron W. West | last post by:
Timings... sometimes there are almost too many ways to do the same thing. The only significant findings I see from all the below timings is: 1) Integer math is generally fastest, naturally....
0
by: Lee Harr | last post by:
I am used to creating sequences separately and using them for default values instead of using the serial type. I am not sure why... but that's the way I have been doing it. Maybe I don't trust the...
2
by: edurne murgiondo via DBMonster.com | last post by:
I have a table with a column as identity. I don't know how can migrate a identity column to a normal column using sequence. Thnaks -- Message posted via http://www.dbmonster.com
7
by: pfa | last post by:
I have a udf which returns a table. I was hoping to access the NEXTVAL from a sequence to affect the logic flow and consequently the return result. I'm using a LANGUAGE C type function. Here's...
4
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...
1
by: filip1150 | last post by:
I'm trying to find if there is any performance diference between explicitly using a sequence in the insert statement to generate values for a column and doing this in an insert trigger. I...
7
by: Rahul B | last post by:
Hi, If i do a "Select nextval for <seq_namefrom sysibm.sysdummy1", it increases the values of nextval by1. How can i find out the nextval of a sequence without actually increasing the value....
0
by: Frank Swarbrick | last post by:
So we're trying to decide if it's better to use IDENTITY columns or sequences to create a surrogate key as the primary key for our tables. I kind of like the identity column, because it's more...
8
by: arachno | last post by:
My Oracle sequences seem to be auto-incrementing themselves "over time". My row ID's are sequenced like this: 1, 4, 5, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 41, etc I'm using...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
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
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...

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.