Loop in postgres-SQL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Copyright © austinmakasare22@gmail.

com

\Loop in postgres-SQL
Basic /simple loop
This is the basic unconditional loop which starts with keyword LOOP and
executes the statements within its body until terminated by an EXIT or
RETURN Statement.
Syntax: <<label>>
loop
statements/body;
end loop;
The optional label can be used by EXIT statements in nested loops to specify
which level of nesting should terminated.
EXIT [ label] [ when expression];
If no label is given, the innermost loop is terminated and the statement
following END LOOP is executed next. The set of statements are executed at
least once before termination of loop.
There should be an EXIT condition in the loop, otherwise the loop will get into
an infinite number of iterations.
Example: The following example shows how to use the loop statement to
print all numbers from 1 to 5.
do $$ declare n
integer: = 6; cnt
integer: = 1; begin
loop exit when cnt =
n; raise notice '%',
cnt; cnt: = cnt + 1;
end loop; end; $$;

2.while Loop:
The WHILE statements repeat a sequence of statements till
the condition remains true and stops executing when the
conditions become false.
Copyright © austinmakasare22@gmail.com
Copyright © austinmakasare22@gmail.com

Syntax:
[ <<label>> ] while
condition loop
statements; end
loop;
Example
The following code is used to raise notices while increasing the count of an
arbitrary variable from 0 to 9.
do $$ declare add integer: =
0; begin while add <10 loop
raise notice 'count %', add;
add: = add+1; end loop;
end$$;

3.FOR Loop:
It is used to execute a set of statements for a fixed number of
times. It is iterated between the start and end integer values.
Syntax:
FOR counter IN initial value ... final value LOOP
LOOP statements;
END LOOP;

Example:
1.The following code uses the for-loop statement to iterate over ten numbers
from 1 to 10 and display each of them in each iteration:
do $$ begin
for cnt in 1..10 loop
raise notice 'cnt: %', cnt;
end loop; end; $$
2. The following code uses the for-loop statement to iterate over ten numbers
from 10 to 1 and display each of them in each iteration:

Copyright © austinmakasare22@gmail.com
Copyright © austinmakasare22@gmail.com

do $$ begin
for cnt in
reverse
10..1 loop
raise
notice
'cnt: %',
cnt; end
loop; end;
$$

4.Lopping through Query Results


Using a different type of FOR loop, we can iterate through the
results of a query and manipulate that data accordingly.
Syntax: [ <<label>>] for
target in query loop
statements end loop [
label ];
Example:
First, we create a sample table using the below commands to perform
examples:
CREATE TABLE employees
(employee_id serial PRIMARY KEY,
full_name VARCHAR NOT NULL,
manager_id INT
);
Then we insert data into our employee table as follows:
INSERT INTO employees (
employee_id,
full_name, manager_id
)
VALUES
(1, 'M.S Dhoni', NULL),
(2, 'Sachin Tendulkar', 1),
(3, 'R. Sharma', 1),
(4, 'S. Raina', 1),
(5, 'B. Kumar', 1),
do $$ declare f
record; begin
Copyright © austinmakasare22@gmail.com
Copyright © austinmakasare22@gmail.com

for f in select employee_id, full_name


from employees
order by employee_id desc, full_name
limit 10 loop
raise notice '% - % ', f.employee_id, f.full_name;
end loop; end; $$;

5.FOR-IN-EXECUTE Statement:
The FOR-IN-EXECUTE Statement is another way to iterate over rows of
record set.
Syntax:

[ <<label>> ]
for row in execute query expression [ using query Param [, ...] ]
loop statements end loop [ label];
• The query expression is an SQL statement.
• The using clause is used to pass the query parameters.

Copyright © austinmakasare22@gmail.com

You might also like