[OR REPLACE] 更新函数介绍

最近更新时间: 2024-10-17 17:10:00

带OR REPLACE的作用就函数存在时则替换的功能,建立PL/pgsql函数时不带OR REPLACE关键字, 则遇到函数已经存系统则会报错,如下所示:

postgres=# select prosrc from pg_proc where proname='f';
          prosrc
--------------------------
                         +
 BEGIN                   +
     RAISE NOTICE 'TBase';+
 END;                    +

(1 行记录)

postgres=# CREATE FUNCTION f() RETURNS VOID AS
postgres-# $$
postgres$# BEGIN
postgres$#     RAISE NOTICE 'Hello ,TBase';
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
ERROR:  function "f" already exists with same argument types
postgres=# CREATE OR REPLACE FUNCTION f() RETURNS VOID AS
postgres-# $$
postgres$# BEGIN
postgres$#     RAISE NOTICE 'Hello ,TBase';
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=# select prosrc from pg_proc where proname='f';
             prosrc
---------------------------------
                                +
 BEGIN                          +
     RAISE NOTICE 'Hello ,TBase';+
 END;                           +

(1 行记录)

postgres=# SELECT f();
NOTICE:  Hello ,TBase
 f
---

(1 行记录)