今天学习openGauss存储过程和函数,对于数据库开发这个功能是比较有份量的,语法看存储过程跟oracle相近,函数有些不一样。
存过语法:https://opengauss.org/zh/docs/2.1.0/docs/Developerguide/CREATE-PROCEDURE.html
CREATE [ OR REPLACE ] PROCEDURE procedure_name
[ ( {[ argmode ] [ argname ] argtype [ { DEFAULT | := | = } expression ]}[,...]) ]
[
{ IMMUTABLE | STABLE | VOLATILE }
| { SHIPPABLE | NOT SHIPPABLE }
| {PACKAGE}
| [ NOT ] LEAKPROOF
| { CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT }
| {[ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER | AUTHID DEFINER | AUTHID CURRENT_USER}
| COST execution_cost
| SET configuration_parameter { TO value | = value | FROM CURRENT }
][ ... ]
{ IS | AS }
plsql_body
/
复制
函数语法:两种风格,贴的是O风格 https://opengauss.org/zh/docs/2.1.0/docs/Developerguide/CREATE-FUNCTION.html
CREATE [ OR REPLACE ] FUNCTION function_name
( [ { argname [ argmode ] argtype [ { DEFAULT | := | = } expression ] } [, ...] ] )
RETURN rettype [ DETERMINISTIC ]
[
{IMMUTABLE | STABLE | VOLATILE }
| {SHIPPABLE | NOT SHIPPABLE}
| {PACKAGE}
| {FENCED | NOT FENCED}
| [ NOT ] LEAKPROOF
| {CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT }
| {[ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER |
AUTHID DEFINER | AUTHID CURRENT_USER
}
| COST execution_cost
| ROWS result_rows
| SET configuration_parameter { {TO | =} value | FROM CURRENT
][...]
{
IS | AS
} plsql_body
/
复制
学习笔记:
- 存储过程:
- 查看存过视图:pg_proc;
- 调用存过:除了原本常规的调用方式,opengauss 还可以使用select 存过;虽然oracle也支持,但两者应该是有区别,oracle不建议在select过程中使用存过,opengauss也只是单纯的select 调用;
- 删除存过:可以使用if exists关键字,来避免删除无该对象的报错。
drop procedure if exists <procedure>;
- 函数:
- 创建函数:有个$$符号,目前不是很清楚作用,可以不用但不确定是否会引起其他问题;
- 查看函数:可用 \sf 函数名 或pg_proc
- 调用函数:
call 函数名,传参支持使用命名标记法传参 CALL func_add_sql(num2 := 2, num1 := 3); 或 CALL func_add_sql(num1 => 1,num2 => 3);
- 返回recode形式:关键语法
returns SETOF RECORD
- 重命名:ALTER FUNCTION [FUNC] () RENAME TO [NEWFUNC];
课后练习:
1.创建带有入参和出参的函数1,调用函数时使用按参数值传递和命名标记法传参
openGauss=# create function func(a in integer,b in integer,res out integer) return integer as begin res:=a*b; end;
openGauss$# /
CREATE FUNCTION
openGauss=# \sf func
CREATE OR REPLACE FUNCTION public.func(a in integer,b in integer,res out integer)
RETURN integer NOT FENCED NOT SHIPPABLE
AS DECLARE begin res:=a*b; end;
/
openGauss=# call func(2,9,c);
res
-----
18
(1 row)
openGauss=# call func(2,9,a);
res
-----
18
(1 row)
openGauss=# call func(a=>2,b=>3,c);
res
-----
6
(1 row)
openGauss=#
复制
2.创建返回类型为record的函数2,重命名函数2
openGauss=# create function fun2(i int,out r1 int,out r2 int) returns setof record as $$ begin r1=i*2;r2=i*3;return next; end; $$language plpgsql;
/
CREATE FUNCTION
openGauss=# call fun2(1,0,0);
r1 | r2
----+----
2 | 3
(1 row)
openGauss=#
复制
openGauss=# alter function fun2(int) rename to func2; --注意参数只要入参,出参不用
ALTER FUNCTION
复制
3.使用\sf和系统函数查看函数定义
openGauss=# \sf func2
CREATE OR REPLACE FUNCTION public.func2(i integer, OUT r1 integer, OUT r2 integer)
RETURNS SETOF record
LANGUAGE plpgsql
NOT FENCED NOT SHIPPABLE
AS $function$ begin r1=i*2;r2=i*3;return next; end; $function$;
openGauss=# select * from pg_proc where proname='func2';
proname | pronamespace | proowner | prolang | procost | prorows | provariadic | protransform | proisagg | proiswindow | prosecdef | pr
oleakproof | proisstrict | proretset | provolatile | pronargs | pronargdefaults | prorettype | proargtypes | proallargtypes | proargmod
es | proargnames | proargdefaults | prosrc | probin | proconfig | proacl | prodefaultargpos | fencedmo
de | proshippable | propackage | prokind | proargsrc | propackageid | proisprivate | proargtypesext | prodefaultargposext
---------+--------------+----------+---------+---------+---------+-------------+--------------+----------+-------------+-----------+---
-----------+-------------+-----------+-------------+----------+-----------------+------------+-------------+----------------+----------
---+-------------+----------------+-----------------------------------------+--------+-----------+--------+------------------+---------
---+--------------+------------+---------+-----------+--------------+--------------+----------------+---------------------
func2 | 2200 | 10 | 11900 | 100 | 1000 | 0 | - | f | f | f | f
| f | t | v | 1 | 0 | 2249 | 23 | {23,23,23} | {i,o,o}
| {i,r1,r2} | | begin r1=i*2;r2=i*3;return next; end; | | | | | f
| f | f | f | | 0 | f | |
(1 row)
openGauss=#
复制
4.删除函数
openGauss=# drop function func;
DROP FUNCTION
openGauss=# drop function func2;
DROP FUNCTION
openGauss=#
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
内蒙古公司成功完成新一代BOSS云原生系统割接上线
openGauss
183次阅读
2025-03-24 09:40:40
第4期 openGauss 中级认证OGCP直播班招生中!3月30日开课
墨天轮小教习
149次阅读
2025-03-17 15:48:40
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
95次阅读
2025-04-01 12:27:03
从数据库源码比较 PostgreSql和OpenGauss的启动过程
maozicb
62次阅读
2025-03-24 15:55:04
一文快速上手openGauss
进击的CJR
59次阅读
2025-03-26 16:12:54
openGauss HASH JOIN原理
lbsswhu
56次阅读
2025-03-18 10:45:01
民生银行测试用例参数
Cabbage
49次阅读
2025-03-05 09:35:20
openGauss 学习之路:集群部署实战探索
openGauss
39次阅读
2025-03-21 10:34:13
openGauss问题记录:开启备机归档且备机stop情况下,执行gs_probackup失败
zym
37次阅读
2025-03-18 19:06:13
openGauss 首届委员会线下联合例会:融合用户视角,共话技术规划
openGauss
30次阅读
2025-03-05 11:09:25