POSTGRESQL PGADMIN4中的存储过程错误

问题描述

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

错误:“过程”处或附近的语法错误

第1行:创建或替换过程转移(

创建表的代码

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;$$

解决方法

仅当您使用9.4版标记问题时,该程序仅在Postgres 11版中可用。

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

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

使用:

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