列引用对于函数中的局部变量不明确

时间:2019-01-23 07:06:27

标签: sql postgresql plpgsql

以下是我的功能

CREATE OR REPLACE FUNCTION  add_config_to_enterprise(configKey character varying, enterpriseValue character varying) RETURNS void AS 
$BODY$
DECLARE
entId character varying;
   BEGIN
        FOR entId IN
    SELECT enterprise_id
    FROM tenant
LOOP
    INSERT INTO enterprise_configuration(enterprise_configuration_id, product_configuration_id, enterprise_id, value)
SELECT *
FROM 
    (SELECT uuid_generate_v4(),
        (SELECT pc.product_configuration_id
        FROM product_configuration pc
        WHERE pc.configuration_key = configKey), 
            entId, enterpriseValue) AS tmp
            WHERE NOT EXISTS 
            (SELECT e.enterprise_configuration_id
            FROM enterprise_configuration e
            WHERE e.enterprise_id = entId AND e.product_configuration_id = 
                (SELECT p.product_configuration_id
                FROM product_configuration p
                WHERE p.configuration_key = configKey));

END LOOP;


   END;

$BODY$ LANGUAGE 'plpgsql'

但是调用此函数时会给我以下错误

ERROR:  column reference "entid" is ambiguous
LINE 12:             WHERE e.enterprise_id = entId AND e.product_conf...
                                             ^
DETAIL:  It could refer to either a PL/pgSQL variable or a table column.

任何表中都没有名为entid的列。为什么会出现这样的错误?

2 个答案:

答案 0 :(得分:2)

void Scale(){ try { PDDocument doc = PDDocument.load(new File(filePath)); for (int p = 0; p < doc.getNumberOfPages(); ++p) { PDRectangle mediaBox = doc.getPage(p).getMediaBox(); mediaBox.transform(Matrix.getScaleInstance(0.5f, 0.5f)); doc.getPage(p).setMediaBox(mediaBox); } doc.save(new File(filePathEdited)); } catch (Exception e) { e.printStackTrace(); } } 条件下使用子查询别名:

WHERE

或在子查询的... SELECT * FROM (SELECT uuid_generate_v4(), (SELECT pc.product_configuration_id FROM product_configuration pc WHERE pc.configuration_key = configKey), entId, enterpriseValue) AS tmp WHERE NOT EXISTS (SELECT e.enterprise_configuration_id FROM enterprise_configuration e WHERE e.enterprise_id = tmp.entId AND e.product_configuration_id = -- here (SELECT p.product_configuration_id FROM product_configuration p WHERE p.configuration_key = configKey)); ... 列表中重命名entId

SELECT

答案 1 :(得分:0)

问题在于,您在查询中定义的别名为tmp的子查询有一个名为entid的列,因此解析器不知道是否引用了entid WHERE条件是指该列或函数变量。

是的,它们包含相同的值,因此在现实中并不重要,但是在语法上仍然存在歧义。

您必须使用别名entid或函数WHERE的名称来限制tmpadd_config_to_enterprise条件下的{{1}},以解决歧义。 / p>

您的查询不必要地复杂且缩进,以至于我无法理解和改进它。简化它可以解决问题。