postgresql - 从 psql 调用带有 inout 参数的过程
问题描述
给定下表
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 做不到的事情)
解决方案
好吧,你会在 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;
$$
;
推荐阅读
- java - 防止应用程序从一台 PC 复制到另一台 PC
- ios - AudioKit:如何避免使用 midi 'through' 和同步音符时对 CPU 造成的巨大冲击?
- vb.net - form.showdialog 然后 form.close / or form.dispose 抛出 System.NullReferenceException: '对象引用未设置为对象的实例。
- javascript - 使用密码学的 NodeJS 数字签名
- java - 使用“var”(局部变量类型推断)声明变量的性能后果
- python - 如果一个 Cron 作业失败,则继续下一个作业
- python - PyQt 小部件在 QVBoxLayout 内的绝对位置
- python - 检查字符串中相邻的重复字符
- django - 计算时间 Django
- beautifulsoup - 从 html 文本中提取表格