指定列名后PostgreSQL模糊列引用

时间:2016-07-05 09:50:56

标签: postgresql

我有这样的功能

CREATE OR REPLACE FUNCTION factors_apart_sa()
  RETURNS TABLE(surf_area integer, sa_factor numeric) AS
$BODY$
BEGIN
    RETURN QUERY SELECT factors_apart_sa.surf_area, factors_apart_sa.sa_factor FROM factors_apart_sa;
END;

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
  ROWS 1000;

调用此函数后,我收到错误:

column reference "factors_apart_sa.surf_area" is ambiguous

如果我指定了表名,为什么它仍然不明确。

这是表factors_apart_sa:

CREATE TABLE factors_apart_sa
(
  factors_apar_sa_id bigserial NOT NULL,
  surf_area integer,
  sa_factor numeric(19,4) NOT NULL
)

1 个答案:

答案 0 :(得分:1)

使用表别名:

CREATE OR REPLACE FUNCTION factors_apart_sa()
  RETURNS TABLE(surf_area integer, sa_factor numeric) AS
$BODY$
BEGIN
    RETURN QUERY SELECT f.surf_area, f.sa_factor FROM factors_apart_sa f;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE;

为表和函数设置相同的名称并不是一个好主意;

要调用函数returnig行,请将其放在FROM子句中:

select f.* 
from factors_apart_sa() f;