2

My goal is to register a user defined function in h2. The second time I execute the program an error, stating that my function alias already exists, appears.

stmt.execute("CREATE ALIAS LEVENSHTEIN FOR
 \"modules.musicProvider.LocalNetworkMusicProvider.computeLevenshteinDistance\"");

After taking a look at INFORMATION_SCHEMA, I can't find a way to check beforehand if the function is already defined or not.

  1. How do figure out if a user defined function is already defined?
  2. How do I delete user defined functions?
Kilian
  • 1,540
  • 16
  • 28

1 Answers1

7

I know this is a bit of an old question, but as I have been working with H2 today in this exact area of user-defined functions I thought I would answer the question.

One option is to always drop the ALIAS before creating it:

DROP ALIAS IF EXISTS LEVENSHTEIN;

I got this information from this StackOverflow question.

Another other option is to not create the alias if it already exists:

CREATE ALIAS IF NOT EXISTS LEVENSHTEIN ...

I got this nugget from this StackOverflow question and the H2 documentation.

Finally, you can also execute this query to see if the ALIAS exists:

SELECT EXISTS(SELECT * FROM INFORMATION_SCHEMA.FUNCTION_ALIASES WHERE ALIAS_NAME = 'LEVENSHTEIN')

I got the basics for this query from this StackOverflow question.