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

Unique sequence object for each row returned from select statement

Hi folks. I need unique sequence object generated for each row returned
form select statement. Here is my ADDID() UDF:

CREATE FUNCTION DB2ADMIN.ADDID()
RETURNS CHAR(6)
NOT DETERMINISTIC
F1: BEGIN ATOMIC
DECLARE ID CHAR(6);
SET ID = 'BB' CONCAT (substr(char(cast(NEXT VALUE for
DB2ADMIN.SEQUENCEID as decimal(4, 0))), 1, 4)) ;
RETURN ID;
END
$

How can I make my function ADDID() get executed mutiple times if rows
returned are more than one in this select statement?

SELECT XML2CLOB(
XMLELEMENT (
NAME "Cars",
XMLELEMENT (NAME "CarMake", CAR_MAKE),
XMLELEMENT (NAME "CarID", DB2ADMIN.ADDID())
)
)
FROM AUTOS WHERE CAR_COLOR='Red';
That is if I got 3 red cars I need unique sequence object generated for
each car.
Right now it assigns the same BB0008 for all 3 cars.

<Cars><CarMake>Ford</CarMake><CarID>BB0008</CarID></Cars>

<Cars><CarMake>BMW</CarMake><CarID>BB0008</CarID></Cars>

<Cars><CarMake>Ferrari</CarMake><CarID>BB0008</CarID></Cars>

Dec 20 '05 #1
5 4520
Antanas wrote:
Hi folks. I need unique sequence object generated for each row returned
form select statement. Here is my ADDID() UDF:

CREATE FUNCTION DB2ADMIN.ADDID()
RETURNS CHAR(6)
NOT DETERMINISTIC
F1: BEGIN ATOMIC
DECLARE ID CHAR(6);
SET ID = 'BB' CONCAT (substr(char(cast(NEXT VALUE for
DB2ADMIN.SEQUENCEID as decimal(4, 0))), 1, 4)) ;
RETURN ID;
END
$

How can I make my function ADDID() get executed mutiple times if rows
returned are more than one in this select statement?

SELECT XML2CLOB(
XMLELEMENT (
NAME "Cars",
XMLELEMENT (NAME "CarMake", CAR_MAKE),
XMLELEMENT (NAME "CarID", DB2ADMIN.ADDID())
)
)
FROM AUTOS WHERE CAR_COLOR='Red';
That is if I got 3 red cars I need unique sequence object generated for
each car.
Right now it assigns the same BB0008 for all 3 cars.

<Cars><CarMake>Ford</CarMake><CarID>BB0008</CarID></Cars>

<Cars><CarMake>BMW</CarMake><CarID>BB0008</CarID></Cars>

<Cars><CarMake>Ferrari</CarMake><CarID>BB0008</CarID></Cars>


How about this:

SELECT row_number() over (), ...
FROM ...

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 20 '05 #2
Knut, nope this is not good for me. I need to increment the sequence,
as each object generated in XML must have unique object id.

Dec 20 '05 #3
Antanas wrote:
Knut, nope this is not good for me. I need to increment the sequence,
as each object generated in XML must have unique object id.


So you need this to be unique across multiple SELECT statements?

Have a look at the GENERATE_UNIQUE() function. Or use sequences combined
with ROW_NUMBER().

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 20 '05 #4
>So you need this to be unique across multiple SELECT statements?
Yes.
use sequences combined with ROW_NUMBER().

Gute Idee! Knut Dank für einen Tip.

Dec 20 '05 #5
>So you need this to be unique across multiple SELECT statements?
Yes.
use sequences combined with ROW_NUMBER().

Gute Idee! Knut Dank für einen Tip.

Dec 20 '05 #6

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

Similar topics

4
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I...
5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
4
by: Kris Rudin | last post by:
I am displaying a table of information on a web page, using an asp:table that I populate dynamically. On this page I give the user the options to group the rows by certain fields, and/or filter the...
7
by: urban.widmark | last post by:
Hello We are having some problems with triggers, sequences and union all in V8 on code that worked fine in V7. Was wondering if someone else has seen this and/or knows what to do. A trigger...
1
by: daldridge | last post by:
I have a unique-elements/sorting question (who doesn't?), but haven't yet been able to get appropriate template/select/for-each processing working. I don't fully grok the Muenchian technique yet...
4
by: jim | last post by:
I've been searching and trying out SQL statements for hours and I'm still having a problem. I'm querying 3 tables... and I only want the first unique record returned. Currently, the data...
10
by: Phil Latio | last post by:
I am inserting data into user table which contains 5 fields, sounds simple enough normally but 2 of the fields are designated as UNIQUE. If someone does enter a value which already exists, how do I...
8
by: Marc | last post by:
Hi all, I have to generate and send to a printer many 6 digit alphanumeric strings. they have to be unique but I cannot check in a database or something like that if it have already been printed....
6
by: Alvin SIU | last post by:
Hi all, I have a table in Db2 v8 like this: Team Name Role ------ -------- --------------------- A Superman Leader A Batman Member A WonderWoman Member B ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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...

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.