我正在编写一个函数,它将选择结果输出并将其求和到一个新表中 - 因此我尝试使用 INTO 函数。但是,我的独立代码有效,但是一旦进入函数,我就会收到一条错误消息,指出新的 SELECT INTO 表不是已定义的变量(也许我遗漏了什么)。请看下面的代码:
CREATE OR REPLACE FUNCTION rev_1.calculate_costing_layer()
RETURNS trigger AS
$BODY$
BEGIN
-- This will create an intersection between pipelines and sum the cost to a new table for output
-- May need to create individual cost columns- Will also keep infrastructure costing seperated
--DROP table rev_1.costing_layer;
SELECT inyaninga_phases.geom, catchment_e_gravity_lines.name, SUM(catchment_e_gravity_lines.cost) AS gravity_sum
INTO rev_1.costing_layer
FROM rev_1.inyaninga_phases
ON ST_Intersects(catchment_e_gravity_lines.geom,inyaninga_phases.geom)
GROUP BY catchment_e_gravity_lines.name, inyaninga_phases.geom;
RETURN NEW;
END;
$BODY$
language plpgsql
最佳答案
根据 the documentation :
CREATE TABLE AS is functionally similar to SELECT INTO. CREATE TABLE AS is the recommended syntax, since this form of SELECT INTO is not available in ECPG or PL/pgSQL, because they interpret the INTO clause differently. Furthermore, CREATE TABLE AS offers a superset of the functionality provided by SELECT INTO.
使用CREATE TABLE AS .
关于PostgreSQL 选择 INTO 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45393727/