Database Reference
In-Depth Information
LOOP
grade := grade * subjects;
END LOOP;
RETURN grade;
END;
$$ LANGUAGE plpgsql;
The result can be seen using the following statement:
warehouse_db=# SELECT first_for_loop(2);
first_for_loop
----------------
2048
(1 row)
You can also play with the REVERSE and BY clauses to create the FOR loop, as follows:
warehouse_db=# CREATE OR REPLACE FUNCTION second_for_loop(subjects
integer)
RETURNS integer AS $$
DECLARE
grade integer := 2;
BEGIN
FOR i IN REVERSE 10..1 BY 2
LOOP
grade := grade * subjects;
END LOOP ;
RETURN grade;
END;
$$ LANGUAGE plpgsql;
The result can be seen using the following statement:
warehouse_db=# SELECT second_for_loop(2);
second_for_loop
-----------------
64
(1 row)
Now we will iterate over a dynamic query (that is unknown at the time of
writing the function and processed on runtime) using the EXECUTE keyword
in the following manner:
warehouse_db=# CREATE OR REPLACE FUNCTION for_loop_query(query
VARCHAR)
RETURNS integer AS $$
DECLARE
 
Search WWH ::




Custom Search