postgres=# -- Using the basic loop
postgres=#
postgres=# CREATE FUNCTION square_integer_loop (integer) RETURNS integer AS '
postgres'# DECLARE
postgres'# -- Declare aliases for function argument.
postgres'# num1 ALIAS FOR $1;
postgres'# -- Declare an integer to hold the result.
postgres'# result integer;
postgres'# BEGIN
postgres'# -- Assign the user input number to the result variable.
postgres'# result := num1;
postgres'# LOOP
postgres'# result := result * result;
postgres'# EXIT WHEN result >= 10000;
postgres'# END LOOP;
postgres'# RETURN result;
postgres'# END;
postgres'# ' LANGUAGE 'plpgsql';
CREATE FUNCTION
postgres=#
postgres=#
postgres=# SELECT square_integer_loop(3);
square_integer_loop
---------------------
43046721
(1 row)
postgres=#