从 psql 调用带有 inout 参数的过程
Calling a procedure with inout parameters from psql
给定以下 table
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 ;
$$ ;
Can/how 可以从 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 做不到的事情)
嗯,你会在 PL/pgSQL 中做同样的事情:
set client_min_messages=notice;
do
$$
declare
a_err text;
a_id int;
begin
call dt_test.integer_insert(a_id, 13, 'A prime example', a_err);
raise notice 'The new ID is: %', a_id;
end;
$$
;
给定以下 table
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 ;
$$ ;
Can/how 可以从 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 做不到的事情)
嗯,你会在 PL/pgSQL 中做同样的事情:
set client_min_messages=notice;
do
$$
declare
a_err text;
a_id int;
begin
call dt_test.integer_insert(a_id, 13, 'A prime example', a_err);
raise notice 'The new ID is: %', a_id;
end;
$$
;