I am getting the following error in PostgreSQL:
[42702] ERROR: column reference "topicid" is ambiguous Detail: It could refer to either a PL/pgSQL variable or a table column. Where: PL/pgSQL function topics(integer) line 3 at RETURN QUERY
I understand that to mean there is a parameter and a table column with the same name. Except that I cannot see where it is because the only parameter I have is _typeid
and not topicid
.
Where is the problem exactly in this function:
CREATE FUNCTION topics(_typeid integer DEFAULT NULL::integer)
RETURNS TABLE(topicid integer, typeid integer, topic character varying)
LANGUAGE plpgsql
AS
$$
BEGIN
RETURN QUERY SELECT
topicid
,typeid
,topic
FROM
topic
WHERE
_typeid IS NULL
OR
typeID = _typeid
ORDER BY
topic ASC;
END
$$;
If I refactor it to just use sql
then it works fine like such:
CREATE FUNCTION topics(_typeid integer DEFAULT NULL::integer)
RETURNS TABLE(topicid integer, typeid integer, topic character varying)
LANGUAGE sql
AS
$$
SELECT
topicid
,typeid
,topic
FROM
topic
WHERE
_typeid IS NULL
OR
typeID = _typeid
ORDER BY
topic ASC;
$$;