Bird
0
0

How can you declare and assign a variable counter that increments by 1 inside a loop in PL/pgSQL?

hard📝 Application Q9 of 15
PostgreSQL - PL/pgSQL Fundamentals
How can you declare and assign a variable counter that increments by 1 inside a loop in PL/pgSQL?
ADECLARE counter integer := 0; BEGIN FOR i IN 1..5 LOOP SET counter = counter + 1; END LOOP; END;
BDECLARE counter integer; BEGIN FOR i IN 1..5 LOOP counter = counter + 1; END LOOP; END;
CDECLARE counter integer := 0; BEGIN FOR i IN 1..5 LOOP counter += 1; END LOOP; END;
DDECLARE counter integer := 0; BEGIN FOR i IN 1..5 LOOP counter := counter + 1; END LOOP; END;
Step-by-Step Solution
Solution:
  1. Step 1: Check variable initialization

    counter must be initialized to 0 before loop.
  2. Step 2: Verify increment syntax inside loop

    DECLARE counter integer := 0; BEGIN FOR i IN 1..5 LOOP counter := counter + 1; END LOOP; END; uses ':=' correctly to increment counter inside loop.
  3. Final Answer:

    Correctly declares and increments counter -> Option D
  4. Quick Check:

    Correct loop increment syntax = A [OK]
Quick Trick: Use ':=' and initialize before loop for increments [OK]
Common Mistakes:
  • Using '=' instead of ':=' inside loop
  • Using '+=' which is invalid in PL/pgSQL
  • Using SET inside loop incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes