How to use sequence in FUNCTION?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dmitry Bond.

    How to use sequence in FUNCTION?

    Hi.

    Please help me. I need to port one stored function from Oracle to DB2.
    In Oracle there is a sequence used, like this:

    SELECT seqTF.nextval INTO seq_nextval FROM dual;

    I have lost 2 hours trying to find how to do the same thing in DB2. :-
    ( Unfortuinately without success.

    I tried following variants:

    VALUES NEXT VALUE FOR seqTF INTO seq_nextval;
    seq_nextval := NEXT VALUE FOR seqTF;
    seq_nextval := (SELECT NEXT VALUE FOR seqTF FROM SYSIBM.SYSDUMMY 1);

    Unfortuinately all variants listed above do not work.

    Could you please help me with it?

    I need it for DB2 8.2.7.
    Function should be as fast as possible, not use any additional tables.

    Regards,
    Dmitry.

    PS. Here is the full code of function I converted from Oracle:

    CREATE FUNCTION GetSysTimeFract ion() RETURNS timestamp
    LANGUAGE SQL
    NOT DETERMINISTIC
    BEGIN ATOMIC
    DECLARE time_str VARCHAR(50);
    DECLARE fraction_str VARCHAR(50);
    DECLARE seq_nextval INTEGER;
    DECLARE tTmStmp TIMESTAMP;

    seq_nextval := NEXT VALUE FOR seqTF;

    tTmStmp := current timestamp;
    time_str := cast(tTmStmp as char(26));
    fraction_str := '000000' || cast(cast(subst r(time_str, 21, 3) as int)
    * 100 + seq_nextval as char(9));
    fraction_str := substr(fraction _str, length(fraction _str) - 6, 6);
    time_str := substr(time_str , 1, 20);
    RETURN cast((time_str || fraction_str) as timestamp);
    END@
Working...