I am getting the below error when I try to create either a stored procedure or a stored function in PGADMIN4. How can I fix it:
ERROR: syntax error at or near "procedure"
LINE 1: create or replace procedure transfer(
The code to create the tables:
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);
Error is received after I add the below stored procedure:
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;$$
Procedures are available in Postgres starting version 11 only, while you tagged your question with version 9.4.
In earlier versions, you can use a void function instead. Basically, replace:
create or replace procedure transfer(sender int, receiver int, amount dec)
language plpgsql
as $$
begin
...
end; $$
With:
create or replace function transfer(sender int, receiver int, amount dec)
returns void
language plpgsql
as $$
begin
...
end; $$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.