Hi,
yes you are right! My first intention was to get some general examples it there were some.
Here is the latest Version i tried to use:
CREATE FUNCTION WINEX(LONG VARCHAR)
RETURNS TABLE (result LONG VARCHAR)
EXTERNAL NAME 'WindowsExec!getResult'
LANGUAGE JAVA
PARAMETER STYLE DB2GENERAL
NOT DETERMINISTIC
FENCED
NO SQL
NO EXTERNAL ACTION
SCRATCHPAD
NO FINAL CALL
DISALLOW PARALLEL
NO DBINFO;
GRANT EXECUTE ON FUNCTION WINEX TO PUBLIC WITH GRANT OPTION;
import COM.ibm.db2.app.*;
import java.io.*;
import java.lang.*;
public class WindowsExec extends UDF {
public void getResult(String str,
String outStr
)
throws Exception
{
int intRow = 0;
byte[] scratchpad = getScratchpad();
// variables to read from SCRATCHPAD area
ByteArrayInputStream
byteArrayIn = new ByteArrayInputStream(scratchpad);
DataInputStream
dataIn = new DataInputStream(byteArrayIn);
// variables to write into SCRATCHPAD area
byte[] byteArrayRow;
int i;
ByteArrayOutputStream
byteArrayOut = new ByteArrayOutputStream(10);
DataOutputStream
dataOut = new DataOutputStream(byteArrayOut);
switch (getCallType())
{
case SQLUDF_TF_FIRST:
// do initialization for the whole statement
// (the statement may invoke tableUDF more than once)
break;
case SQLUDF_TF_OPEN:
// do initialization valid for this invokation of tableUDF
intRow = 1;
// save data in SCRATCHPAD area
dataOut.writeInt(intRow);
byteArrayRow = byteArrayOut.toByteArray();
for(i = 0; i < byteArrayRow.length; i++)
{
scratchpad[i] = byteArrayRow[i];
}
setScratchpad(scratchpad);
break;
case SQLUDF_TF_FETCH:
// get data from SCRATCHPAD area
intRow = dataIn.readInt();
// work with data
if(intRow > 1) //MOE
{
// Set end-of-file signal and return
setSQLstate ("02000");
}
else
{
// Set the current output row and increment the row number
set(2, str);
intRow++;
}
// save data in SCRATCHPAD area
dataOut.writeInt(intRow);
byteArrayRow = byteArrayOut.toByteArray();
for(i = 0; i < byteArrayRow.length; i++)
{
scratchpad[i] = byteArrayRow[i];
}
setScratchpad(scratchpad);
break;
case SQLUDF_TF_CLOSE:
break;
case SQLUDF_TF_FINAL:
break;
}
}
}