参数默认值

最近更新时间: 2025-02-18 16:02:00

PL/pgsql扩展语言函数支持给参数设置默认值。

  • 复制
    复制成功
postgres=# CREATE OR REPLACE FUNCTION f7 (a_int INTEGER DEFAULT 1) RETURNS VOID AS
postgres-# $$
postgres$# BEGIN
postgres$#     RAISE NOTICE 'a_int = %',a_int;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f7();
NOTICE:  a_int = 1
 f7
----
(1 行记录)
#备注:如果原来存在一个f7()这样的函数,则上面的执行就会出错,因为系统无法清楚到你到底要执行那个函数,如下所示
postgres=# CREATE OR REPLACE FUNCTION f7()  RETURNS void AS
postgres-# $$
postgres$# BEGIN
postgres$#     RAISE NOTICE '无参数';
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql ;
CREATE FUNCTION
postgres=#
postgres=# SELECT * FROM f7();
ERROR:  function f7() is not unique
第1行SELECT * FROM f7();
                   ^
#提示:  Could not choose a best candidate function. You might need to add explicit type casts.
postgres=#

出错提示,f7()函数不是唯一的,这是使用上一个需要特别注意的地方。