IF...THEN...END IF
teledb=# CREATE OR REPLACE FUNCTION f26() RETURNS VOID AS
teledb-# $$
teledb$# BEGIN
teledb$# IF random()>0.5 THEN
teledb$# RAISE NOTICE '随机数大于0.5';
teledb$# END IF;
teledb$# END;
teledb$# $$
teledb-# LANGUAGE plpgsql;
CREATE FUNCTION
teledb=# select f26();
NOTICE: 随机数大于0.5
f26
-----
(1 row)
teledb=#
IF...THEN...ELSE...END IF
teledb=# CREATE OR REPLACE FUNCTION f26() RETURNS VOID AS
teledb-# $$
teledb$# BEGIN
teledb$# IF random()>0.99 THEN
teledb$# RAISE NOTICE '随机数大于0.99';
teledb$# ELSE
teledb$# RAISE NOTICE '随机数小于或等于0.99';
teledb$# END IF;
teledb$# END;
teledb$# $$
teledb-# LANGUAGE plpgsql;
CREATE FUNCTION
teledb=# select f26();
NOTICE: 随机数小于或等于0.99
f26
-----
(1 row)
teledb=#
IF...THEN...ELSIF...THEN...ELSE...END IF
teledb=# CREATE OR REPLACE FUNCTION f26() RETURNS VOID AS
teledb-# $$
teledb$# DECLARE
teledb$# v_float8 float8 := random();
teledb$# BEGIN
teledb$# IF v_float8>0.99 THEN
teledb$# RAISE NOTICE '随机数大于0.99';
teledb$# ELSIF v_float8>0.5 THEN
teledb$# RAISE NOTICE '随机数大于0.50';
teledb$# ELSIF v_float8>0.25 THEN
teledb$# RAISE NOTICE '随机数大于0.25';
teledb$# ELSE
teledb$# RAISE NOTICE '随机数小于或等于0.25';
teledb$# END IF;
teledb$# END;
teledb$# $$
teledb-# LANGUAGE plpgsql;
CREATE FUNCTION
teledb=# SELECT f26();
NOTICE: 随机数大于0.50
f26
-----
(1 row)
CASE 语句
teledb=# CREATE OR REPLACE FUNCTION f26() RETURNS VOID AS
teledb-# $$
teledb$# DECLARE
teledb$# v_float8 float8 := random();
teledb$# BEGIN
teledb$# CASE
teledb$# WHEN v_float8>0.99 THEN
teledb$# RAISE NOTICE '随机数大于0.99';
teledb$# WHEN v_float8>0.5 THEN
teledb$# RAISE NOTICE '随机数大于0.50';
teledb$# WHEN v_float8>0.25 THEN
teledb$# RAISE NOTICE '随机数大于0.25';
teledb$# ELSE
teledb$# RAISE NOTICE '随机数小于或等于0.25';
teledb$# END CASE;
teledb$# END;
teledb$# $$
teledb-# LANGUAGE plpgsql;
CREATE FUNCTION
teledb=# SELECT f26();
NOTICE: 随机数大于0.50
f26
-----
(1 row)