應用程序語法介紹
更新時間 2025-02-14 10:24:07
最近更新時間: 2025-02-14 10:24:07
分享文章
您可以通過本頁了解天翼云TeleDB數據庫的建立函數語法、[OR REPLACE] 更新函數介紹、[模式名.]函數名介紹。
建立函數語法
CREATE [OR REPLACE] FUNCTION [模式名.]函數名 ([參數模式 [參數名] 數據類型 [default 默認值] [,…]]) RETRUNS [SETOF] 數據類型 AS
[標簽]
[DECLARE
--變量定義]
BEGIN
--注釋
/*注釋*/
--語句執行
END;
[標簽]
LANGUAGE PLPGSQL;[OR REPLACE] 更新函數介紹
OR REPLACE 的作用為函數存在時則替換,建立 PL/pgsql 函數時如果不帶 OR REPLACE 關鍵字, 則遇到函數已經存在,系統會報錯,如下所示:
teledb=# select prosrc from pg_proc where proname='f';
prosrc
--------------------------------
+
BEGIN +
RAISE NOTICE 'Hello ,teledb';+
END; +
(1 row)
teledb=# CREATE FUNCTION f() RETURNS VOID AS
$$
BEGIN
RAISE NOTICE 'Hello ,teledb';
END;
$$
LANGUAGE PLPGSQL;
ERROR: function "f" already exists with same argument types
teledb=# CREATE OR REPLACE FUNCTION f() RETURNS VOID AS
$$
BEGIN
RAISE NOTICE 'Hello ,teledb';
END;
$$
LANGUAGE PLPGSQL;
CREATE FUNCTION
teledb=# select prosrc from pg_proc where proname='f';
prosrc
--------------------------------
+
BEGIN +
RAISE NOTICE 'Hello ,teledb';+
END; +
(1 row)
teledb=# select f();
NOTICE: Hello ,teledb
f
---
(1 row)[模式名.]函數名介紹
建立函數名稱,模式名可以指定,也可以不指定,不指存則存放在當前模式下,如上面例子就沒有指定模式名,則就存放在當前模式下,如下所示:
teledb=# select * from pg_namespace;
nspname | nspowner | nspacl
--------------------+----------+----------------------------------
pg_toast | 10 |
pg_oracle | 10 |
squeeze | 10 |
pg_temp_1 | 10 |
pg_toast_temp_1 | 10 |
pg_catalog | 10 | {teledb=UC/teledb,=U/teledb}
public | 10 | {teledb=UC/teledb,=UC/teledb}
information_schema | 10 | {teledb=UC/teledb,=U/teledb}
(8 rows)
teledb=# show search_path;
search_path
-----------------
"$user", public
(1 row)
teledb=# select pg_namespace.nspname,pg_proc.prosrc from pg_proc,pg_namespace where
teledb-# pg_proc.pronamespace=pg_namespace.oid and pg_proc.proname='f';
nspname | prosrc
---------+--------------------------------
public | +
| BEGIN +
| RAISE NOTICE 'Hello ,teledb';+
| END; +
|
(1 row)因為$user模式不存在,所以存在public模式下。