Bird
0
0

Consider this PL/pgSQL snippet:

medium📝 query result Q13 of 15
PostgreSQL - Advanced PL/pgSQL
Consider this PL/pgSQL snippet:
DECLARE
  table_name text := 'users';
  rec_count int;
BEGIN
  EXECUTE 'SELECT count(*) FROM ' || table_name INTO rec_count;
  RETURN rec_count;
END;

What will this function return when called if the users table has 10 rows?
ANULL
BAn error because of missing quotes
C0
D10
Step-by-Step Solution
Solution:
  1. Step 1: Understand dynamic SQL concatenation

    The query string becomes 'SELECT count(*) FROM users', which is valid SQL.
  2. Step 2: EXECUTE runs the query and stores result

    EXECUTE runs the query and puts the count into rec_count variable.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Count rows in users = 10 [OK]
Quick Trick: Concatenate table name safely, EXECUTE runs query, INTO stores result [OK]
Common Mistakes:
  • Expecting error due to missing quotes around table name
  • Assuming rec_count stays NULL without assignment
  • Confusing dynamic SQL with static SQL syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes