IF...THEN...END IF
teledb=# CREATE OR REPLACE PROCEDURE p_if() AS
$$
BEGIN
IF random()>0.5 THEN
RAISE NOTICE '随机数大于0.5';
END IF;
END;
$$
LANGUAGE plpgsql;
CREATE PROCEDURE
teledb=# CALL p_if();
NOTICE: 随机数大于0.5
CALL
teledb=#
..THEN...ELSE...END IF
teledb=# CREATE OR REPLACE PROCEDURE p_if_else() AS
$$
BEGIN
IF random()>0.99 THEN
RAISE NOTICE '随机数大于0.99';
ELSE
RAISE NOTICE '随机数小于或等于0.99';
END IF;
END;
$$
LANGUAGE plpgsql;
CREATE PROCEDURE
teledb=# CALL p_if_else();
NOTICE: 随机数小于或等于0.99
CALL
teledb=#
IF...THEN...ELSIF...THEN...ELSE...END IF
teledb=# CREATE OR REPLACE PROCEDURE p_if_elsif() AS
$$
DECLARE
v_float8 float8 := random();
BEGIN
IF v_float8>0.99 THEN
RAISE NOTICE '随机数大于0.99';
ELSIF v_float8>0.5 THEN
RAISE NOTICE '随机数大于0.50';
ELSIF v_float8>0.25 THEN
RAISE NOTICE '随机数大于0.25';
ELSE
RAISE NOTICE '随机数小于或等于0.25';
END IF;
END;
$$
LANGUAGE plpgsql;
CREATE PROCEDURE
teledb=# CALL p_if_elsif();
NOTICE: 随机数大于0.50
CALL
CASE 语句
teledb=# CREATE OR REPLACE PROCEDURE p_case() AS
$$
DECLARE
v_float8 float8 := random();
BEGIN
CASE
WHEN v_float8>0.99 THEN
RAISE NOTICE '随机数大于0.99';
WHEN v_float8>0.5 THEN
RAISE NOTICE '随机数大于0.50';
WHEN v_float8>0.25 THEN
RAISE NOTICE '随机数大于0.25';
ELSE
RAISE NOTICE '随机数小于或等于0.25';
END CASE;
END;
$$
LANGUAGE plpgsql;
CREATE PROCEDURE
teledb=# CALL p_case();
NOTICE: 随机数小于或等于0.25
CALL
teledb=#