不明确的列引用 - 它可以引用 PL/pgSQL 变量或 table 列?
Ambiguous column reference - it could refer to either a PL/pgSQL variable or a table column?
我在 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
我理解这意味着有一个参数和一个同名的 table 列。除了我看不到它在哪里,因为我唯一的参数是 _typeid
而不是 topicid
.
这个函数的问题到底在哪里:
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
$$;
如果我将它重构为仅使用 sql
那么它可以像这样正常工作:
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;
$$;
冲突发生在RETURNS TABLE
中的变量和查询返回的字段名之间。 Table 限定查询中的字段名称,例如topic.topicid, topic.typid, topic.topic
.
我在 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
我理解这意味着有一个参数和一个同名的 table 列。除了我看不到它在哪里,因为我唯一的参数是 _typeid
而不是 topicid
.
这个函数的问题到底在哪里:
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
$$;
如果我将它重构为仅使用 sql
那么它可以像这样正常工作:
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;
$$;
冲突发生在RETURNS TABLE
中的变量和查询返回的字段名之间。 Table 限定查询中的字段名称,例如topic.topicid, topic.typid, topic.topic
.