Hi,

H2 supports sequences directly, no need to write complicated code as with
MySQL. See also http://h2database.com/html/grammar.html#create_sequence

Create a sequence:
create sequence abc;

Getting the next value:
select next value for abc;
or
select abc.nextval;

Regards,
Thomas




On Thu, Aug 29, 2013 at 1:22 PM, Rajiv Perera <[email protected]> wrote:

> I am very new to H2 and I went through some of the documents but I still
> cannot convert the following MySql sequence to H2 sequence.
>
> in MySql I have the following sequence:
>
> CREATE DEFINER=`root`@`localhost` FUNCTION `PK_SEQUENCE`() RETURNS int(11)
> BEGIN
> DECLARE sequence_val INTEGER;
>     insert into PK_SEQUENCE values (NULL);
>     SELECT LAST_INSERT_ID() INTO sequence_val;
>     -- delete from sequence;
>     return sequence_val;
> END
>
> In the Java code I use the following :
>
> public class PKSequenceService
> {
>     public static int getPKSequence(EJFrameworkConnection connection)
>     {
>
>         EJStatementExecutor executor = new EJStatementExecutor();
>         List<EJSelectResult> results = executor.executeQuery(connection,
> "SELECT PK_SEQUENCE() AS ID");
>
>         for (EJSelectResult result : results)
>         {
>             Object itemValue = result.getItemValue("ID");
>             if (itemValue instanceof Integer)
>             {
>                 return ((Integer) itemValue).intValue();
>             }
>         }
>
>         throw new RuntimeException("Can not create PKSequence.");
>     }
>
> }
>
> In the database I have a table named "PK_SEQUENCE()". CAN ANY ONE HELP ME
> TO CONVERT THIS sequence in to H2 sequence ?
> The basic thing I does is use this sequence to generate the next integer
> when called from the Java code.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "H2 Database" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/h2-database.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to