PostgreSQL 选择 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 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/

相关文章:

python-3.x - 带有包含 % 符号的 LIKE 子句的 psycopg2 insert 语句

mysql - SQL : ERROR 1005: Can't create table 'obl2.itemsubjects' (errno: 121)

mysql - 检索现有表的 CREATE TABLE 代码?

java - 在 Restful 客户端中检索枚举的 PostgreSQL 类型

sql - 带有 WHERE 子句的 PostgreSQL Upsert

postgresql - 使用 Docker/Docker compose 运行 Postgres 迁移

当我运行 python manage.py syncdb 时,Django 避免在数据库中创建 PointField

postgresql - 输入几何具有未知(0)几何(尽管使用了 st_transform)

postgresql - 在以下情况下如何使用 ST_Contains?

mysql - 在 PHPMyAdmin 中合并两个表