PostgreSQL 11-过程 [英] PostgreSQL 11 - Procedures

查看:129
本文介绍了PostgreSQL 11-过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有PostgreSQL支持程序的最新更新.官方博客援引 相对于函数,过程不需要返回值." (

With the latest update of PostgreSQL supporting procedures. The official blog, quoted that "As opposed to functions, procedures are not required to return a value." (https://blog.2ndquadrant.com/postgresql-11-server-side-procedures-part-1/)

所以我的问题是,实际上我有什么方法可以在过程中返回错误代码或响应吗? (由于Postgres中的程序比较新,因此在线资源很少.)

So my question is, is there actually any way for me to return error code or response in a procedure? (Procedures is rather new in Postgres thus there were very little resources online.)

以下是我返回这些错误代码"的意思的示例

Here is an example of what I meant by returning these "error codes"

create or replace PROCEDURE multislot_Update_v1
(
  p_id in varchar2,
  p_name in varchar2,
  p_enname in varchar2,
  results out SYS_REFCURSOR
) AS
rowNumber int;
defaultNumber int;
BEGIN

   select count(1) into rowNumber from MULTISLOTSGAME where fid=P_id;

    if (rowNumber = 0) then
      open results for
      select '1' as result from dual;
      return;
    end if;

  update MULTISLOTSGAME  set
    name = P_name,
    enname = P_enname
  where fid = P_id ;
  commit;

 open results for
  select '0' as result, t1.* from MULTISLOTSGAME t1 where fid = p_id;

END multislot_Update_v1;

上面的脚本是一个Oracle过程,因为您可以看到返回的结果是否为"1",这意味着更新不成功.

The above script is an Oracle procedure, as u can see if the returned result is "1" it meant that the update wasn't successful.

有什么办法可以将上述脚本(带有错误代码)编写为PostgresSQL过程?也许使用"INOUT"参数的示例会很棒!

Is there any way I can write the above script (with error code) as a PostgresSQL Procedure ? Maybe an example of using the "INOUT" argument would be great!

推荐答案

您可以在过程中使用INOUT参数.

You can have INOUT parameters in a procedure.

您使用 CALL 语句调用过程;如果有任何INOUT参数,该语句将返回与SELECT类似的结果行.

You call a procedure with the CALL statement; if there are any INOUT parameters, the statement will return a result row just like SELECT.

以下是使用返回refcursor的过程的示例:

Here is an example that uses a procedure that returns a refcursor:

CREATE PROCEDURE testproc(INOUT r refcursor) LANGUAGE plpgsql AS
$$BEGIN
   r := 'cur';
   OPEN r FOR VALUES (1), (42), (12321);
END;$$;

BEGIN;

CALL testproc(NULL);

  r  
-----
 cur
(1 row)

FETCH ALL FROM cur;

 column1 
---------
       1
      42
   12321
(3 rows)

COMMIT;

这篇关于PostgreSQL 11-过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆