POSTGRESQL PGADMIN4 中的存储过程错误

Stored Procedure error in POSTGRESQL PGADMIN4

当我尝试在 PGADMIN4 中创建存储过程或存储函数时出现以下错误。我该如何解决:

ERROR: syntax error at or near "procedure"

LINE 1: create or replace procedure transfer(

创建表的代码:

drop table if exists accounts;
create table accounts (
    id int generated by default as identity,
    name varchar(100) not null,
    balance dec(15,2) not null,
    primary key(id)
);
insert into accounts(name,balance) values('Bob',10000);
insert into accounts(name,balance) values('Alice',10000);

添加以下存储过程后出现错误:

create or replace procedure transfer(
   sender int,
   receiver int, 
   amount dec
)
language plpgsql    
as $$
begin
    -- subtracting the amount from the sender's account 
    update accounts 
    set balance = balance - amount 
    where id = sender;

    -- adding the amount to the receiver's account
    update accounts 
    set balance = balance + amount 
    where id = receiver;

    commit;
end;$$

程序仅在 Postgres 11 版开始可用,而您使用 9.4 版标记了您的问题。

在早期版本中,您可以改用 void 函数。基本上,替换:

create or replace procedure transfer(sender int, receiver int, amount dec)
language plpgsql    
as $$
begin
    ...
end; $$

与:

create or replace function transfer(sender int, receiver int, amount dec)
returns void
language plpgsql    
as $$
begin
    ...
end; $$