Skip to content
Advertisement

How to create a function that returns a value if id is in an array?

I’m trying to create a new function in supabase. I’m quit new to SQL and postgreSQL so I’m not sure what I’m doing. Anyway I want to make a function that checks if one value is in an array of ints. If it is then return 0.8 if it’s not then return 1.0.

Here is my code I get the error message “Failed to run sql query: syntax error at or near “begin””

Advertisement

Answer

You are missing a ; after the variable declaration and all other statements. And as documented in the manual the assignment operator in PL/pgSQL is := (or =), not SET.

As also documented in the manual an IF needs an END IF.

So the correct syntax would be:

However you don’t need the variable or the EXISTS at all. There is also no need to refer to parameters by their number

In fact you don’t even need PL/pgSQL for this:

Advertisement