Introduction
Loops help you repeat actions many times without writing the same code again and again.
Jump into concepts and practice - no test required
LOOP -- statements END LOOP; WHILE condition LOOP -- statements END LOOP; FOR record_variable IN query LOOP -- statements END LOOP;
LOOP RAISE NOTICE 'Hello!'; EXIT; END LOOP;
DO $$ DECLARE counter INT := 1; BEGIN WHILE counter <= 3 LOOP RAISE NOTICE 'Count: %', counter; counter := counter + 1; END LOOP; END $$;
FOR rec IN SELECT id, name FROM users LOOP RAISE NOTICE 'User: % - %', rec.id, rec.name; END LOOP;
DO $$ DECLARE counter INT := 1; BEGIN WHILE counter <= 5 LOOP RAISE NOTICE 'Number: %', counter; counter := counter + 1; END LOOP; END $$;
LOOP statement do in PostgreSQL procedural code?LOOP in PostgreSQL repeats the code inside it indefinitely until an EXIT command is used to stop it.WHILE or FOR, LOOP does not check a condition automatically; it relies on EXIT to stop.EXIT command is reached. -> Option DFOR loop iterating over rows from a query in PostgreSQL?FOR record IN SELECT ... LOOP to iterate over query rows.DECLARE
counter INT := 1;
total INT := 0;
BEGIN
WHILE counter <= 3 LOOP
total := total + counter;
counter := counter + 1;
END LOOP;
RAISE NOTICE 'Total: %', total;
END;DECLARE
i INT := 1;
BEGIN
WHILE i < 5 LOOP
RAISE NOTICE '%', i;
END LOOP;
END;i is never increased inside the loop, so the condition i < 5 never becomes false.i inside the loop. -> Option Aprice column from a products table using a FOR loop in PL/pgSQL. Which code snippet correctly does this?FOR rec IN SELECT price FROM products LOOP, which is correct syntax to iterate query results.rec.price to total and then outputs total with RAISE NOTICE.