PL/pgSQL函数带output参数例子

时间:2022-06-26 17:36:53

例子1,不带returns :

[postgres@cnrd56 bin]$ ./psql
psql (9.1.2)
Type "help" for help.

postgres=# CREATE FUNCTION sales_tax(subtotal real, OUT tax real) AS $$
postgres$# BEGIN
postgres$#     tax := subtotal * 0.06;
postgres$# END;
postgres$# $$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# 
postgres=# select sales_tax(100);
 sales_tax 
-----------
         6
(1 row)

例子2,带returns:

[postgres@cnrd56 bin]$ ./psql
psql (9.1.2)
Type "help" for help.

postgres=# CREATE FUNCTION sales_tax2(subtotal real, OUT tax real) returns real AS $$
postgres$# BEGIN
postgres$#     tax := subtotal * 0.06;
postgres$# END;
postgres$# $$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# 
postgres=# select sales_tax2(100);
 sales_tax2 
------------
          6
(1 row)

postgres=#