定义CONSTANT变量

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

  • 复制
    复制成功
postgres=# CREATE OR REPLACE FUNCTION f25() RETURNS VOID AS
postgres-# $$
postgres$# DECLARE    
postgres$#     v_int CONSTANT integer := 1;     
postgres$# BEGIN
postgres$#     RAISE NOTICE 'v_int = %',v_int;  
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# select f25();
NOTICE:  v_int = 1
 f25 
-----
(1 row)
#CONSTANT 不能再次赋值
postgres=# CREATE OR REPLACE FUNCTION f25() RETURNS VOID AS
postgres-# $$
postgres$# DECLARE    
postgres$#     v_int CONSTANT integer := 1;     
postgres$# BEGIN
postgres$#     RAISE NOTICE 'v_int = %',v_int;  
postgres$#     v_int = 10;
postgres$#     RAISE NOTICE 'v_int = %',v_int;       
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
ERROR:  "v_int" is declared CONSTANT