1.创建带有入参和出参的函数1,调用函数时使用按参数值传递和命名标记法传参
create or replace function test1 (i in int, o out int) return int
as
begin
o = i*2;
end;
/
omm=# call test1(i=>2,o=>0);
o
---
4
(1 row)
omm=# omm=#
2.创建返回类型为record的函数2,重命名函数2
create or replace function test2(i int, out res_1 bigint, out res_2 bigint)
returns SETOF RECORD
as $$
begin
res_1 = i * 2;
res_2 = i * 3;
return next;
end;
$$language plpgsql;
alter function test2(int) rename to test3;
3.使用\sf和系统函数查看函数定义
omm=# \sf test3
omm=# CREATE OR REPLACE FUNCTION public.test3(i integer, OUT res_1 bigint, OUT res_2 bigint)
RETURNS SETOF record
LANGUAGE plpgsql
NOT FENCED NOT SHIPPABLE
AS $function$
begin
res_1 = i * 2;
res_2 = i * 3;
return next;
end;
$function$;
omm=# select * from pg_proc where proname='test3';
proname | pronamespace | proowner | prolang | procost | prorows | provariadic | protransform | proisagg | proiswindow | prosecdef | proleakproof | proisstrict | proretset | provolatile | pronargs | pronargdefaults | prorettype | proargtyp
es | proallargtypes | proargmodes | proargnames | proargdefaults | prosrc | probin | proconfig | proacl | prodefaultargpos | fencedmode | proshippable | propackage | prokind
| {23,20,20} | {i,o,o} | {i,res_1,res_2} | | +| | | | | f | f | f | f
---------+--------------+----------+---------+---------+---------+-------------+--------------+----------+-------------+-----------+--------------+-------------+-----------+-------------+----------+-----------------+------------+----------
---+----------------+-------------+-----------------+----------------+----------------+--------+-----------+--------+------------------+------------+--------------+------------+---------
test3 | 2200 | 10 | 11750 | 100 | 1000 | 0 | - | f | f | f | f | f | t | v | 1 | 0 | 2249 | 23
| | | | | | | | | | | | | | | | | |
| | | | | begin +| | | | | | | |
| | | | | res_1 = i * 2;+| | | | | | | |
| | | | | | | | | | | | | | | | | |
| | | | | res_2 = i * 3;+| | | | | | | |
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | |
| | | | | return next; +| | | | | | | |
| | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | |
(1 row)
| | | | | end; +| | | | | | | |
| | | | | | | | | | | | | | | |
4.删除函数
drop function test1;
drop function test3;
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




