13

This question is reference to question "How to create stored procedure using H2 database?" (continuation of the same question).

Is there a way to write a function in H2 without using Java code? My requirement is to translate the functions written in SQL to H2 without using Java code. I found lot of examples in different portals doing the same using Java code. Your help will be greatly appreciated.

Regards Arun

4
  • 3
    I don't think this is possible in H2. You might want to look at HSQLDB which supports procedures in its own SQL dialect and does not require Java for that. Btw: what do you mean with "written in SQL"? Which DBMS is you source? (SQL is only a query language it is not a DBMS product). Commented Jun 25, 2013 at 9:32
  • 1
    If you look here at the h2 Documentation it states "In addition to the built-in functions, this database supports user-defined Java functions". i.e. you create your procedures as java code then create aliases for them. Commented Jun 25, 2013 at 9:36
  • Thanks for the quick response. Iam using MsSqlDatabase and the function is written for MsSqlDB. Then the only wayout to write procedures in H2 is through Java Code..? Commented Jun 25, 2013 at 9:37
  • @JavaDevil : Thank you. I read it but believed that there will be a tweak for it. :( . Commented Jun 25, 2013 at 9:38

2 Answers 2

18

Currently, H2 does only support functions written in Java or a related language (for example Groovy or Scala). PL/SQL (Oracle) and T-SQL (MS SQL Server, Sybase) are not supported.

Sign up to request clarification or add additional context in comments.

2 Comments

Does H2 still doesn't support Stored Procedure so our query doesn't have to be recompiled every time and rest of the features of SP?
@AbdulJabbarWebBestow sorry I don't understand your question.
5

If your primary goal is to run SQL (or SQLesque) statements within your H2 Java user-defined functions, jOOQ could be an option as a "PL/Java" implementation. Of course, this would still be a Java solution.

An example of such a function can be seen in this blog post:

http://blog.jooq.org/2011/11/04/use-jooq-inside-your-h2-database

public class Functions {
    public static int countBooks(Connection connection, Integer authorId) 
    throws SQLException {
        // Translate your T-SQL statements to jOOQ statements
        return DSL.using(connection, SQLDialect.H2)
                  .selectCount()
                  .from(BOOK)
                  .where(BOOK.AUTHOR_ID.eq(authorId))
                  .fetchOne(0, int.class);
    }
}

Declare the above method as an ALIAS to H2

CREATE ALIAS countBooks 
   FOR "org.example.Functions.countBooks";

Use the function in SQL

SELECT author.last_name, countBooks(author.id) 
FROM author

A similar approach can be taken with H2's own SQL abstraction JaQu, of course. Using JaQu wouldn't add any additional dependency, I think.

1 Comment

Just what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.