循环语句

最近更新时间: 2024-06-12 15:06:00

以下几个示例,说明循环语句的类型以及使用方法。

LOOP循环

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# DECLARE
postgres$#      v_id INTEGER := 1;
postgres$# BEGIN           
postgres$#     LOOP       
postgres$#         RAISE NOTICE '%',v_id;
postgres$#         EXIT WHEN random()>0.8;
postgres$#         v_id := v_id + 1;
postgres$#     END LOOP ;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27();
NOTICE:  1
NOTICE:  2
 f27 
-----

(1 row)

使用EXIT退出循环

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# DECLARE
postgres$#     v_id INTEGER := 1;
postgres$#     v_random float8 ;
postgres$# BEGIN           
postgres$#     LOOP       
postgres$#        RAISE NOTICE '%',v_id;
postgres$#        v_id := v_id + 1;
postgres$#        v_random := random();
postgres$#        IF v_random > 0.8 THEN
postgres$#            RETURN;
postgres$#        END IF;
postgres$#     END LOOP ;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27();
NOTICE:  1
NOTICE:  2
NOTICE:  3
NOTICE:  4
NOTICE:  5
 f27 
-----

(1 row)

postgres=# 

使用RETURN退出循环返回

WHILE循环

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# DECLARE
postgres$#     v_id INTEGER := 1;
postgres$#     v_random float8 := random() ;
postgres$# BEGIN           
postgres$#     WHILE v_random > 0.8 LOOP
postgres$#         RAISE NOTICE '%',v_id;
postgres$#         v_id := v_id + 1;
postgres$#         v_random = random();
postgres$#     END LOOP;  
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27();
NOTICE:  1
 f27 
-----

(1 row)

FOR循环

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# BEGIN           
postgres$#     FOR i IN 1..3 LOOP
postgres$#         RAISE NOTICE 'i = %',i;
postgres$#     END LOOP;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27();
NOTICE:  i = 1
NOTICE:  i = 2
NOTICE:  i = 3
 f27 
-----

(1 row)

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# BEGIN           
postgres$#     FOR i IN REVERSE 3..1 LOOP
postgres$#         RAISE NOTICE 'i = %',i;
postgres$#     END LOOP;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27();
NOTICE:  i = 3
NOTICE:  i = 2
NOTICE:  i = 1
 f27 
-----

(1 row)

使用REVERSE递减

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# BEGIN           
postgres$#     FOR i IN 1..8 BY 2 LOOP
postgres$#         RAISE NOTICE 'i = %',i;
postgres$#     END LOOP;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27(); 
NOTICE:  i = 1
NOTICE:  i = 3
NOTICE:  i = 5
NOTICE:  i = 7
 f27 
-----

(1 row)

使用BY设置步长

FOR循环查询结果

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# DECLARE
postgres$#     v_rec RECORD;
postgres$# BEGIN           
postgres$#     FOR v_rec IN SELECT * FROM public.t LOOP
postgres$#         RAISE NOTICE '%',v_rec;
postgres$#     END LOOP;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27();
NOTICE:  (1,TDSQL PG)
NOTICE:  (2,pgxz)
 f27 
-----

(1 row)

FOREACH循环一个数组

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# DECLARE
postgres$#     v_random_arr float8[]:=ARRAY[random(),random()];
postgres$#     v_random float8;
postgres$# BEGIN           
postgres$#     FOREACH v_random IN ARRAY v_random_arr LOOP
postgres$#         RAISE NOTICE '%',v_random ;
postgres$#     END LOOP;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27();
NOTICE:  0.452758576720953
NOTICE:  0.975814974401146
 f27 
-----

(1 row)

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# DECLARE
postgres$#     v_random_arr float8[][]:=ARRAY[ARRAY[random(),random()],ARRAY[random(),random()]];
postgres$#     v_random float8;
postgres$# BEGIN           
postgres$#     FOREACH v_random SLICE 0 IN ARRAY v_random_arr LOOP
postgres$#         RAISE NOTICE '%',v_random ;
postgres$#     END LOOP;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27();
NOTICE:  0.0588191924616694
NOTICE:  0.368828620761633
NOTICE:  0.813376842066646
NOTICE:  0.415377039927989
 f27 
-----

(1 row)

循环会通过计算expression得到的数组的个体元素进行迭代

postgres=# CREATE OR REPLACE FUNCTION f27() RETURNS VOID AS
postgres-# $$    
postgres$# DECLARE
postgres$#     v_random_arr float8[][]:=ARRAY[ARRAY[random(),random()],ARRAY[random(),random()]];
postgres$#     v_random float8[];
postgres$# BEGIN           
postgres$#     FOREACH v_random SLICE 1 IN ARRAY v_random_arr LOOP
postgres$#         RAISE NOTICE '%',v_random ;
postgres$#     END LOOP;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27();
NOTICE:  {0.578366641886532,0.78098024148494}
NOTICE:  {0.783956411294639,0.450278480071574}
 f27 
-----

(1 row)

通过一个正SLICE值,FOREACH通过数组的切片而不是单一元素迭代