Wha*_*ark 1 postgresql postgis create-table insert-into
我正在编写一个函数,它将选择结果输出并将其汇总到一个新表中 - 因此我尝试使用 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
Run Code Online (Sandbox Code Playgroud)
根据文档:
CREATE TABLE AS 在功能上类似于 SELECT INTO。CREATE TABLE AS 是推荐的语法,因为这种形式的 SELECT INTO 在 ECPG 或 PL/pgSQL 中不可用,因为它们对 INTO 子句的解释不同。此外,CREATE TABLE AS 提供了 SELECT INTO 提供的功能的超集。