How can I make this pseudo code to work in Postgresql:
create or replace function getf(arg character varying(255)) returns int
as $$
if arg = 'a' then return 1;
else return 2;
$$ language sql;
Based on argument I need to return some values and there is no other table I need to query. Just need to build a logic inside function. Tried to replace if with when clause, but could not figure out how to do it.
Thanks!
create or replace function getf(arg character varying(255)) returns int as $$
begin
if arg = 'a' then
return 1;
else
return 2;
end if;
end; $$ language plpgsql;
Note that this is a PL/pgSQL function.
The online manual has an excellent chapter on PL/pgSQL. That should provide everything you need to get started writing procedural function with ample support for logical branching.
Using sql
language, you can do it using case when:
create or replace function getf(arg character varying(255)) returns int as
$$
select case
when arg = 'a'
then 1
else 2
end
$$ language sql;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With