Bird
0
0

Find the problem in this function:

medium📝 Debug Q7 of 15
PostgreSQL - PL/pgSQL Fundamentals
Find the problem in this function:
CREATE FUNCTION test() RETURNS SETOF int AS $$
DECLARE
  i int := 1;
BEGIN
  WHILE i <= 3 LOOP
    RETURN NEXT i;
  END LOOP;
  RETURN;
END;
$$ LANGUAGE plpgsql;
AMissing RETURN statement at the end.
BFunction should return TABLE, not SETOF int.
CRETURN NEXT cannot be used inside loops.
DInfinite loop because variable i is never incremented.
Step-by-Step Solution
Solution:
  1. Step 1: Check loop control variable

    Variable i is initialized but never incremented inside the loop.
  2. Step 2: Consequence of no increment

    Loop condition i <= 3 is always true, causing infinite loop.
  3. Final Answer:

    Infinite loop due to missing increment of i -> Option D
  4. Quick Check:

    Loop variable not updated causes infinite loop = Infinite loop because variable i is never incremented. [OK]
Quick Trick: Always update loop variable to avoid infinite loops [OK]
Common Mistakes:
  • Assuming RETURN NEXT causes loop exit
  • Thinking RETURN needed after RETURN NEXT inside loop
  • Confusing RETURNS SETOF with RETURNS TABLE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes