【发布时间】:2021-12-14 15:05:05
【问题描述】:
给定下表
CREATE TABLE dt_test.dt_integer (
id serial NOT NULL,
test_col integer,
test_comment varchar,
CONSTRAINT dt_integer_pk PRIMARY KEY ( id ) ) ;
以及插入数据的过程
CREATE PROCEDURE dt_test.integer_insert (
a_id inout int,
a_integer in integer,
a_comment in varchar,
a_err inout varchar ( 200 ) )
SECURITY DEFINER
LANGUAGE plpgsql
AS $$
DECLARE
BEGIN
WITH inserted AS (
INSERT INTO dt_test.dt_integer (
test_col,
test_comment )
VALUES (
a_integer,
a_comment )
RETURNING id
)
SELECT id
INTO a_id
FROM inserted ;
END ;
$$ ;
可以/如何从 psql 调用该过程?在 Oracle 中,使用 sql-plus 如下所示:
DECLARE
a_err varchar2 ( 200 ) ;
a_id number ;
BEGIN
dt_test.integer_insert ( a_id, 13, 'A prime example', a_err ) ;
dbms_output.put_line ( 'The new ID is: ' || a_id ) ;
END ;
(我不想认为 sql-plus 可以做 psql 做不到的事情)
【问题讨论】:
-
附带说明:CTE 并不是必需的:
insert into ... returning into a_id;也可以。
标签: postgresql psql