我已经看到许多有关同一错误消息的问题,但是找不到具有相同用例的问题。这是我的以下架构。我基本上需要我的函数来返回插入到sparkSession.read.csv("filename")
表中的整个记录。请注意,我希望稍后使用Postgraphile调用此函数。 cf. custom mutation I have used here
base.batch
我正在调用以下函数:
CREATE SCHEMA base;
CREATE TABLE base.indicator_group (
id SERIAL PRIMARY KEY,
indicator_group TEXT NOT NULL UNIQUE,
created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO base.indicator_group (indicator_group) VALUES ('test');
CREATE TABLE base.batch (
id SERIAL PRIMARY KEY,
status TEXT NOT NULL,
created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
indicator_group_id INTEGER NOT NULL REFERENCES base.indicator_group(id)
);
CREATE OR REPLACE FUNCTION base.execute_indicator_group(id integer)
RETURNS base.batch AS $$
BEGIN
INSERT INTO base.batch (status, indicator_group_id) VALUES ('Pending', 1)
RETURNING *;
END;
$$ LANGUAGE plpgsql VOLATILE STRICT SECURITY DEFINER;
它返回错误:
错误:查询没有结果数据的目的地上下文:PL / pgSQL SQL语句中的函数base.execute_indicator_group(integer)第3行
答案 0 :(得分:2)
使用简单的SQL函数:
CREATE OR REPLACE FUNCTION base.execute_indicator_group(id integer)
RETURNS base.batch AS $$
INSERT INTO base.batch (status, indicator_group_id) VALUES ('Pending', 1)
RETURNING *;
$$ LANGUAGE sql STRICT SECURITY DEFINER;
在FROM
子句中使用它:
SELECT * FROM base.execute_indicator_group(1);
id | status | created_date | updated_date | indicator_group_id
----+---------+----------------------------+----------------------------+--------------------
1 | Pending | 2018-08-06 23:43:15.076431 | 2018-08-06 23:43:15.076431 | 1
(1 row)
在plpgsql函数中(如果您想在函数内部另外执行其他操作),您需要一个变量:
CREATE OR REPLACE FUNCTION base.execute_indicator_group(id integer)
RETURNS base.batch AS $$
DECLARE
result base.batch;
BEGIN
INSERT INTO base.batch (status, indicator_group_id) VALUES ('Pending', 1)
RETURNING *
INTO result;
RETURN result;
END;
$$ LANGUAGE plpgsql VOLATILE STRICT SECURITY DEFINER;