Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / December 2005

Tip: Looking for answers? Try searching our database.

Unique sequence object for each row returned from select statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Antanas - 20 Dec 2005 09:59 GMT
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>
Knut Stolze - 20 Dec 2005 13:11 GMT
> Hi folks. I need unique sequence object generated for each row returned
> form select statement. Here is my ADDID() UDF:
[quoted text clipped - 31 lines]
>
> <Cars><CarMake>Ferrari</CarMake><CarID>BB0008</CarID></Cars>

How about this:

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

Signature

Knut Stolze
DB2 Information Integration Development
IBM Germany

Antanas - 20 Dec 2005 13:59 GMT
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.
Knut Stolze - 20 Dec 2005 15:15 GMT
> 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().

Signature

Knut Stolze
DB2 Information Integration Development
IBM Germany

Antanas - 20 Dec 2005 18:53 GMT
>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.
Antanas - 20 Dec 2005 18:53 GMT
>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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.