Hello.
1.
Write simple generic external UDF that returns nextval (or prevval) for
any given sequence.
For example, java UDF:
-------------------------------
import java.sql.*;
public class DynSeq {
static final String PREV = "P";
static final String NEXT = "N";
public static int getInt(String seqName, String val_type) throws
Exception {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try
{
con = DriverManager.getConnection( "jdbc:default:connection" );
st = con.createStatement();
rs = st.executeQuery
(
"SELECT "+(NEXT.equals(val_type)?"NEXT":"PREV")+"VAL FOR
"+seqName+" "+
"FROM SYSIBM.SYSDUMMY1"
);
return rs.next()?rs.getInt(1):0;
} catch (SQLException ex)
{
throw ex;
} finally
{
if (st!=null) st.close();
if (rs!=null) rs.close();
if (con!=null) con.close();
}
}
}
-------------------------------
2.
Place comiled java class into sqllib/function directory.
-------------------------------
Register it:
3.
create function dynseq (varchar(128), varchar(1))
external name 'DynSeq.getInt'
returns integer
fenced not null call
not deterministic reads sql data
language java parameter style java
disallow parallel;
-------------------------------
4.
Use this function in your function like this:
SET VARSQL = CONCAT('Load_Id_Parm=',CHAR(dynseq('MYSCHEMA.LOAD_ ID',
'N')));
-------------------------------
Sincerely,
Mark B.
Hi,
I managed to get the below function running:
CREATE FUNCTION MYSCHEMA.SEQLOAD_ID()
RETURNS VARCHAR(20)
F1: BEGIN ATOMIC
DECLARE VARSQL VARCHAR(20);
SET VARSQL = CONCAT('Load_Id_Parm=',CHAR(NEXTVAL FOR
MYSCHEMA.LOAD_ID));
RETURN(VARSQL);
END
It needs to be modified a bit.
It should accept the sequence name as an input parameter rather than hard
coded (in the above function, it is hardcoded LOAD_ID)
I am getting errors telling me 'execute immediate' is not allowed.
Any ideas, suggestions?
Thanks.
Cheers,
San.