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

Incrementing sequences in the same select statement

The problem is that when AddID is used multiple times in the same
select statement, it returns the same value in all places. How could I
force function AddID to increment OBJECTID sequence?

Here is a basic sample of usage:
SELECT AddID(), AddID(), AddID(), column1 from table1
Here is how AddID looks like:

CREATE FUNCTION Admin.AddID()
RETURNS INTEGER
F1: BEGIN ATOMIC
DECLARE I INTEGER;
SET I = (NEXT VALUE FOR OBJECTID);
RETURN I;
END

Nov 12 '05 #1
10 7061
Les's assume, that multiple call to AddID() within the same select is
incrementing the sequence. The next question is: what was generated
(hint: the order of incrementing)?

You can increment the sequence and assign generated values to variables
sequentially outside of the select.

But, what is your real (business) need for that?

-- Artur Wronski

Nov 12 '05 #2
I need incremented values to be displayed inside select statement.
Outside of select statement incrementation and assignment to variables
is not an option for me.

Nov 12 '05 #3
Antanas wrote:
The problem is that when AddID is used multiple times in the same
select statement, it returns the same value in all places. How could I
force function AddID to increment OBJECTID sequence?

Here is a basic sample of usage:
SELECT AddID(), AddID(), AddID(), column1 from table1
Here is how AddID looks like:

CREATE FUNCTION Admin.AddID()
RETURNS INTEGER
F1: BEGIN ATOMIC
DECLARE I INTEGER;
SET I = (NEXT VALUE FOR OBJECTID);
RETURN I;
END


I already said in the other news group that you could use an external
function. And here is the code I just wrote to verify this:

---------------------------------------------------------------
import java.sql.*;

public class Test extends COM.ibm.db2.app.UDF {
public void getNextSequenceId(int value) throws Exception {
Connection con = getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("VALUES NEXT VALUE FOR seq");
while (rs.next()) {
value = rs.getInt(1);
}
rs.close();
stmt.close();
set(1, value);
}
}
---------------------------------------------------------------
CREATE SEQUENCE seq@

CREATE FUNCTION getNextSeqValue()
RETURNS INTEGER
EXTERNAL NAME 'Test.getNextSequenceId'
LANGUAGE JAVA
PARAMETER STYLE DB2GENERAL
NOT DETERMINISTIC
NO EXTERNAL ACTION
READS SQL DATA
ALLOW PARALLEL@
---------------------------------------------------------------
$ db2 "values ( getnextseqvalue(), getnextseqvalue(), getnextseqvalue() )"

1 2 3
----------- ----------- -----------
9 8 7

1 record(s) selected.

---------------------------------------------------------------

If you want to, you can parameterize the name of the sequence used or you
hard-wire it like I did.

But as Artur said, you might want to explain what you need this for,
especially if you consider that you have no guarantee of the execution
order for the single occurences of the function. In my case above, you see
that the first function is actually called last - again, that's nothing you
can rely on!

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #4
Antanas wrote:
The problem is that when AddID is used multiple times in the same
select statement, it returns the same value in all places. How could I
force function AddID to increment OBJECTID sequence?

Here is a basic sample of usage:
SELECT AddID(), AddID(), AddID(), column1 from table1
Here is how AddID looks like:

CREATE FUNCTION Admin.AddID()
RETURNS INTEGER
F1: BEGIN ATOMIC
DECLARE I INTEGER;
SET I = (NEXT VALUE FOR OBJECTID);
RETURN I;
END

Anatanas,

I'm confused.
To me it looks like you posted te solution to your problem yourself.
When you use:
SELECT NEXT VALUE FOR s1 AS X, NEXT VALUES FOR s1 AS Y FROM T
You will receive the same values for X and Y.
(On the "why" I refer you to Oracle. I'd have done it differently.....)
When you wrap the NEXT VALUES FOR s1 into a UDF the invocations are
decoupled and each call to the UDF will give you a different value.
During parsing DB2 detects the multiple sequence expressions and
transforms the query into:
SELECT nv AS X, nv AS Y FROM (SELECT NEXT VALUE FOR s1 AS nv FROM T)
When the UDF is present this code doens't kick it (quite intentionally I
might add).

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
Serge Rielau wrote:
Antanas wrote:
The problem is that when AddID is used multiple times in the same
select statement, it returns the same value in all places. How could I
force function AddID to increment OBJECTID sequence?

Here is a basic sample of usage:
SELECT AddID(), AddID(), AddID(), column1 from table1
Here is how AddID looks like:

CREATE FUNCTION Admin.AddID()
RETURNS INTEGER
F1: BEGIN ATOMIC
DECLARE I INTEGER;
SET I = (NEXT VALUE FOR OBJECTID);
RETURN I;
END
Anatanas,

I'm confused.


Me too. ;-)
To me it looks like you posted te solution to your problem yourself.
When you use:
SELECT NEXT VALUE FOR s1 AS X, NEXT VALUES FOR s1 AS Y FROM T
You will receive the same values for X and Y.
(On the "why" I refer you to Oracle. I'd have done it differently.....)
When you wrap the NEXT VALUES FOR s1 into a UDF the invocations are
decoupled and each call to the UDF will give you a different value.


That's due to the BEGIN ATOMIC ... END block? Or just a general UDF thing?

I just saw the above and thought that it did work as the OP said and returns
the same value for both calls to the function. But that is not the case as
I just learned/verified. So it's even easier than my external function
approach.

$ db2 "create sequence seq"
$ db2 "create function x() returns integer begin atomic declare i int; set i
= ( next value for seq ); return i; end"
$ db2 "select x(), x() from sysibm.sysdummy1"

1 2
----------- -----------
1 2

1 record(s) selected.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #6
I was surprised myself today when AddID() started incrementing value
when used multiple times in SELECT statement. Previously it would not,
no matter what I did. The reason for this miracle might be because I
restored my DB from backup, and previous db image might have had some
inconsistency.

Nov 12 '05 #7
I am using this ADDID() function mutliple time in a single query with
SQL/XML functions.

Nov 12 '05 #8
I was surprised myself today when AddID() started incrementing value
when used multiple times in SELECT statement. Previously it would not,
no matter what I did. The reason for this miracle might be because I
restored my DB from backup, and previous db image might have had some
inconsistency.

I am using it SQL/XML queries:
SELECT XML2CLOB (XMLELEMENT (NAME "Element1",
XMLELEMENT (NAME "OBJECTID", ADDID()),
XMLELEMENT (NAME "OBJECTID", ADDID()),
XMLELEMENT (NAME "OBJECTID", ADDID())
)
)
FROM TABLE1 WHERE COLUMN1 =1;

Output:
<Element1><OBJECTID>35</OBJECTID><OBJECTID>34</OBJECTID><OBJECTID>33</OBJECTID></Element1>
I notice that when statement is used with XMLAGG and XMLCONCAT, it
still does produce duplicate values:

SELECT XML2CLOB (XMLELEMENT (NAME "Element1",
XMLAGG( XMLCONCAT(
XMLELEMENT (NAME "OBJECTID", ADDID()),
XMLELEMENT (NAME "OBJECTID", ADDID()),
XMLELEMENT (NAME "OBJECTID", ADDID())
)
)
)

)
FROM TABLE1;

Output:
<Element1><OBJECTID>85</OBJECTID><OBJECTID>84</OBJECTID><OBJECTID>83</OBJECTID><OBJECTID>85</OBJECTID><OBJECTID>84</OBJECTID><OBJECTID>83</OBJECTID></Element1>

Nov 12 '05 #9
Antanas wrote:
I was surprised myself today when AddID() started incrementing value
when used multiple times in SELECT statement. Previously it would not,
no matter what I did. The reason for this miracle might be because I
restored my DB from backup, and previous db image might have had some
inconsistency.

I am using it SQL/XML queries:
SELECT XML2CLOB (XMLELEMENT (NAME "Element1",
XMLELEMENT (NAME "OBJECTID", ADDID()),
XMLELEMENT (NAME "OBJECTID", ADDID()),
XMLELEMENT (NAME "OBJECTID", ADDID())
)
)
FROM TABLE1 WHERE COLUMN1 =1;

Output:
<Element1><OBJECTID>35</OBJECTID><OBJECTID>34</OBJECTID><OBJECTID>33</OBJECTID></Element1>
I notice that when statement is used with XMLAGG and XMLCONCAT, it
still does produce duplicate values:

SELECT XML2CLOB (XMLELEMENT (NAME "Element1",
XMLAGG( XMLCONCAT(
XMLELEMENT (NAME "OBJECTID", ADDID()),
XMLELEMENT (NAME "OBJECTID", ADDID()),
XMLELEMENT (NAME "OBJECTID", ADDID())
)
)
)

)
FROM TABLE1;

Output:
<Element1><OBJECTID>85</OBJECTID><OBJECTID>84</OBJECTID><OBJECTID>83</OBJECTID><OBJECTID>85</OBJECTID><OBJECTID>84</OBJECTID><OBJECTID>83</OBJECTID></Element1>

Mark the function as NOT DETERMINISTIC.
Prior to V8.2 we did not allow SQL Functions to lie.
We have since decided that determinism is in the eye of the beholder.
So DB2 allows you to define a function as DETERMINISTIC (default!) which
isn't really (NEXT VALUE ...).
Reason being that you might use that value to play e.g. with a
scratch-file without the randomness "leaking" out of the function.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #10
Serge and Knut, thanks for such responsive DB2 support!

Antanas

Nov 12 '05 #11

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

Similar topics

2
by: John Wilkinson | last post by:
Hi, I am new to XSLT. My problem is that I wish to create an HTML table, and give each row an incrementing number from 1.This would increment every itteration of a for-each loop. The XSLT...
10
by: Vilson farias | last post by:
Greetings, I'm getting a big performance problem and I would like to ask you what would be the reason, but first I need to explain how it happens. Let's suppose I can't use sequences (it seams...
4
by: Sune Nielsen | last post by:
Hello All! Im suffering an appearantly common problem with psql, although I haven't been able to locate the proper solution. Using phpPgAdmin I've created a relation (note: this relation is...
4
by: MaRcElO PeReIrA | last post by:
Hi guys, I have been using the following table (short, short, short version): CREATE TABLE products ( prod_id SERIAL, description TEXT );
2
by: dan | last post by:
I have 2 tables, tab1 ( integer incremented sequence , col2, col3 ) and tab2 ( integer from tab1, col4, col5 ). When I call this function to add a record to each table: LOOP select...
18
by: Bruno Baguette | last post by:
Hello, I have to design a table wich will store some action reports. Each report have an ID like this 1/2004, 2/2004, ... and each years, they restart to 1 (1/2004, 1/2005, 1/2006,...). So, I...
5
by: Michael Fuhr | last post by:
I'd like to propose that certain GRANTs on a table cascade to the table's implicit sequences. In the current implementation (as of 7.4.5 and 8.0.0beta3), a table owner must typically issue GRANT...
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...
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.