473,394 Members | 2,100 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.

To generate Unique id for every SELECT statement

I am posting this here, just in case anyone may need this.

Step 1:
CREATE SEQUENCE ID_SEQ
START WITH 1050000
INCREMENT BY 1
MAXVALUE 9999999
NO CYCLE
NO CACHE
ORDER

Step 2:
create function uniq()
returns integer
begin atomic
declare i int;
set i = ( next value for db2inst1.id_seq );
return i;
end

Step 3:

select uniq() from sysibm.sysdummy1
--will generate unique id's using the sequence and function described
above.

Nov 12 '05 #1
1 11179
hi****@gmail.com wrote:
I am posting this here, just in case anyone may need this.

Step 1:
CREATE SEQUENCE ID_SEQ
START WITH 1050000
INCREMENT BY 1
MAXVALUE 9999999
NO CYCLE
NO CACHE
ORDER

Step 2:
create function uniq()
returns integer
begin atomic
declare i int;
set i = ( next value for db2inst1.id_seq );
return i;
end

Step 3:

select uniq() from sysibm.sysdummy1
--will generate unique id's using the sequence and function described
above.


I've been working on a similar problem : how to produce a GUID (globally
unique ID) within DB2 for use where sometimes data is being produced on
Windows machines with GUIDs and sometimes is being produced elsewhere and
we have to generate the GUIDs at insert time.

I discovered a Java class, actually part of the Java garbage collection
functionality, which produces a "VMID" which is guaranteed unique across
any virtual machine. So I wrapped this in code to produce a genGUID()
UDF. Here's the UDF definition -

CREATE FUNCTION myfuncs.genGUID()
RETURNS CHAR(50)
EXTERNAL NAME 'genGUID!genGUID'
LANGUAGE JAVA
PARAMETER STYLE JAVA
NOT VARIANT
FENCED
CALLED ON NULL INPUT
NO SQL
NO EXTERNAL ACTION@

To define this, put the above into genGUID.db2 and run -

db2 -td@ -vf genGUID.db2

The Java code for the genGUID class is -

import java.lang.*; Â* Â* Â* Â* Â*// for String class
import java.rmi.dgc.VMID; Â* Â*// for VMID generator class

public class genGUID
{
Â* // scalar UDF
Â* public static String genGUID()
Â* throws Exception
Â* {
Â* Â* String outGUID = new java.rmi.dgc.VMID().toString();

Â* Â* return outGUID;
Â* }
}

To compile this code you put this into genGUID.java and run -

javac genGUID.java

This produces a file genGUID.class, which you then copy into
~db2inst1/function. Â* If you change the code and replace the class it
doesn't take effect until you refresh the DB2 class library -

db2 "call sqlj.refresh_classes()"

Then running the UDF gives -

bash-2.05b$ db2 "values myfuncs.genguid()"

1
--------------------------------------------------
8149868854a78ac2:3384e52c:10747918e3c:-7ff0

Â* 1 record(s) selected.

The actual value produced is 43 characters, but I'm defining the return as
50 to give a bit leeway in case they change it later.

Knocking that lot together was a pleasant evening's diversion on Linux last
night. Thankfully the same code works on the AIX box at work this morning
too.(AIX box has Java 1.3.1, whereas on Linux it was Java 1.5.0).

HTH

Phil Nelson
teamdbaNOATSPAMscotdb.com
Nov 12 '05 #2

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

Similar topics

3
by: David Berry | last post by:
Hi All. I'm trying to write an ASP page that shows me the UNIQUE account number for a customer (so I can pass it to another page) based on a search criteria. For example, I want to do a select...
10
by: Mamuninfo | last post by:
Hello, Have any function in the DB2 database that can generate unique id for each string like oracle, mysql,sybase,sqlserver database. In mysql:- select md5(concat_ws("Row name")) from...
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...
6
by: Sebastien | last post by:
Hi, I am building a products database, linking sales and production. Each part has a unique sales Stock Code and Production Number. The sales stock code is a combination of letters and numbers...
29
by: Lauren Wilson | last post by:
Does anyone know how the following info is extracted from the user's computer by a Front Page form? HTTP User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107...
8
by: mortb | last post by:
Hi, How do I write a GenerateHashcode function that will generate guaranteed unique hashcodes for my classes? cheers, mortb
3
by: isis | last post by:
Hi all, I have been googling ard for days and I am still unable to find a solution to generate ids uniquely hence I would like to appeal for urgent help here. I have a table and for this...
13
by: gamernaveen | last post by:
I am coding a script , where basically the user has to enter his name , choose file , file comments if required and upload. The file comments , name , filename will be stored in the database with...
1
by: situ | last post by:
Hi, I'm using DB2 V9 for windows I'm inserting records into DGTT from select statement; the problem is how to insert unique value for every row inserted. I tried using “generated always as”...
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?
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
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
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.