mipadi
mipadi

Reputation: 410552

Why do I get a syntax error when creating a PostgreSQL function?

I am trying to create a function in PostgreSQL using this query:

create function cs.has_double_bridge(cs.nose_bridge_type) returns boolean as $$
  return $1 = 'double bridge';
$$ language plpgsql stable;

But I keep getting an error on the second line:

ERROR:  syntax error at or near "return"
LINE 2:   return $1 = 'keyhole';
          ^

I am not an expert in PostgreSQL, so I haven't been able to figure out the cause of the error. What am I doing wrong here?

Upvotes: 0

Views: 54

Answers (1)

user330315
user330315

Reputation:

As documented in the manual PL/pgSQL requires a BEGIN ... END block:

create function cs.has_double_bridge(cs.nose_bridge_type) 
  returns boolean 
as $$
begin
  return $1 = 'double bridge';
end;
$$ language plpgsql stable;

Alternatively you can use a SQL function instead:

create function cs.has_double_bridge(cs.nose_bridge_type) 
  returns boolean 
as $$
  select $1 = 'double bridge';
$$ language sql stable;

Upvotes: 1

Related Questions