存储过程

本章我们将讨论存储过程。

创建存储过程

使用 CREATE PROCEDURE 语句创建存储过程,语法如下:

create [or replace] procedure procedure_name(parameter_list)
language plpgsql
as $$
declare
-- 声明变量
begin
-- 存储过程主体
end $$;
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);

以下示例用于将指定金额从一个账户转账到另一个账户:

调用存储过程:

call transfer(1,2,1000);

删除存储过程

删除存储过程的语法:

删除存储过程与删除函数非常相似,包括如果发生重载,需要通过参数列表来显式指定要删除的重载版本。

要查看完整内容,请先登录