PostgreSQL选择进入函数
我正在编写一个函数,它将选择结果输出并将其求和到一个新表中,因此我尝试使用into函数。然而,我的独立代码是可以工作的,但是一旦将一个地方放入一个函数中,我就会得到一个错误,指出新的SELECT into表不是一个定义的变量(也许我遗漏了什么)。请参阅下面的代码:PostgreSQL选择进入函数,postgresql,postgis,create-table,insert-into,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 a
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
Per:
创建表在功能上类似于SELECT INTO。CREATETABLE AS是推荐的语法,因为这种形式的SELECT INTO在ECPG或PL/pgSQL中不可用,因为它们对INTO子句的解释不同。此外,createtableas提供了selectinto提供的功能的超集
使用。根据:
创建表在功能上类似于SELECT INTO。CREATETABLE AS是推荐的语法,因为这种形式的SELECT INTO在ECPG或PL/pgSQL中不可用,因为它们对INTO子句的解释不同。此外,createtableas提供了selectinto提供的功能的超集
使用。尽管
选择。。。新表
是有效的PostgreSQL,它的使用已被弃用(或至少“未推荐”)。它在PL/PGSQL中根本不起作用,因为INSERT-INTO
用于将结果放入变量中
如果要创建新表,应改用:
CREATE TABLE rev_1.costing_layer AS
SELECT
inyaninga_phases.geom, catchment_e_gravity_lines.name, SUM(catchment_e_gravity_lines.cost) AS gravity_sum
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;
如果表已经创建,您只想在其中插入新行,则应使用:
INSERT INTO
rev_1.costing_layer
(geom, name, gravity_sum)
-- Same select than before
SELECT
inyaninga_phases.geom, catchment_e_gravity_lines.name, SUM(catchment_e_gravity_lines.cost) AS gravity_sum
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;
在触发器函数中,您不太可能每次都创建一个新表,因此,我猜您希望执行
INSERT
,而不是创建表。。。作为
尽管选择了。。。新表是有效的PostgreSQL,它的使用已被弃用(或至少“未推荐”)。它在PL/PGSQL中根本不起作用,因为INSERT-INTO
用于将结果放入变量中
如果要创建新表,应改用:
CREATE TABLE rev_1.costing_layer AS
SELECT
inyaninga_phases.geom, catchment_e_gravity_lines.name, SUM(catchment_e_gravity_lines.cost) AS gravity_sum
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;
如果表已经创建,您只想在其中插入新行,则应使用:
INSERT INTO
rev_1.costing_layer
(geom, name, gravity_sum)
-- Same select than before
SELECT
inyaninga_phases.geom, catchment_e_gravity_lines.name, SUM(catchment_e_gravity_lines.cost) AS gravity_sum
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;
在触发器函数中,您不太可能每次都创建一个新表,因此,我猜您希望执行INSERT
,而不是创建表。。。作为