Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2007
    Posts
    2

    Unanswered: Need help in creating JAVA UDF

    Hi all,
    I'm newbie on java programming.

    I'm trying to develop a JAVA UDF that returns the n-1 value passed to it (the previous), I think that using SCRATCHPAD is a good idea.

    Here is my java code for a UDF that simply returns the integer value you pass it:

    CREATE FUNCTION testIt(INTEGER) RETURNS INTEGER
    EXTERNAL NAME 'ACFTestFunc!testIt'
    not fenced
    language java
    parameter style db2general
    no sql
    scratchpad
    no external action


    import java.sql.*;
    class ACFTestFunc extends COM.ibm.db2.app.UDF
    {
    public void testIt(int inValue, int result)
    throws Exception
    {
    set(2,inValue);
    }
    }

    Someone colud help me to modify this code ?

    Thanks a lot
    Ciao

    Alessandro

  2. #2
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    No need for the scratchpad. Just add a class member to store the last value.
    Code:
    class ACFTestFunc extends COM.ibm.db2.app.UDF
    {
        int lastValue;
        public void testIt(int inValue, int result) throws Exception
        {
            set(2, lastValue);
            lastValue = inValue;
        }
    }
    You may want to use the FINAL CALL clause in the function declaration so that you can initialize "lastValue" to something on the first invocation of the UDF.

    Also note that the object will be destroyed at the end of the SQL statement execution. So you can carry values between one call to the function to the next call - within the same SQL statement. (It may work across SQL statements because DB2 does some caching, but it is unreliable at best.)
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  3. #3
    Join Date
    Apr 2007
    Posts
    2
    Hi Stolze,
    now works fine. Thank you for your very quick and accurate reply.

    Ciao und vielen danke !

    Alessandro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •