我有一个存储过程,应该执行以下操作:
我的存储过程代码如下:
CREATE OR REPLACE PROCEDURE insert_landed_trades_tp (timestamp without time zone, varchar(200), varchar(200), varchar(200), json)
LANGUAGE plpgsql
AS $$
DECLARE
next_id bigint;
BEGIN
SELECT nextval('trades_landed_tp_row_id_seq') INTO next_id;
INSERT INTO trades_landed_tp (row_id, capture_dt, trade_guid, feed_name, connector_name, payload, status_code)
VALUES (next_id, $1, $2, $3, $4, $5, 1);
COMMIT;
SELECT next_id;
END;
$$;
我现在要调用此存储过程并使用插入记录的ID:
CALL insert_landed_trades_tp (timezone('utc'::text, now()), 'giud'::varchar(200), 'feed'::varchar(200),'connector'::varchar(200), '{ "customer": "John Doe", "items": {"product": "Beer","qty": 6}}'::json);
在PgAdmin中,以上给出了以下错误:
ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead
所以,我的问题是:
请注意,一旦存储过程开发完成,便会从应用程序(使用Python或C#)而不是PgAdmin中调用该过程。
答案 0 :(得分:1)
存储过程不能使用输出参数,只能使用INOUT。您的过程应如下所示:
CREATE OR REPLACE PROCEDURE insert_landed_trades_tp (
capture_dt timestamp without time zone,
trade_guid varchar(200),
feed_name varchar(200),
connector_name varchar(200),
payload json,
next_id INOUT bigint)
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO trades_landed_tp (row_id, capture_dt, trade_guid, feed_name, connector_name, payload, status_code)
SELECT nextval('trades_landed_tp_row_id_seq'),
capture_dt,
trade_guid,
feed_name,
connector_name,
payload,
1
RETURNING row_id INTO next_id;
COMMIT;
END;
$$;
CALL insert_landed_trades_tp (timezone('utc'::text, now()), 'giud'::varchar(200), 'feed'::varchar(200),'connector'::varchar(200), '{ "customer": "John Doe", "items": {"product": "Beer","qty": 6}}'::json, null);
next_id
---------
1
答案 1 :(得分:0)
该错误消息归因于以下行:
选择next_id;
我建议更改程序的功能,为序列值添加输出参数,并删除“ SELECT next_id;”。