Bird
0
0

What is wrong with this dynamic SQL usage?

medium📝 Debug Q7 of 15
PostgreSQL - Advanced PL/pgSQL
What is wrong with this dynamic SQL usage?
DECLARE
  col text := 'price';
  total numeric;
BEGIN
  EXECUTE format('SELECT sum(%L) FROM sales', col) INTO total;
  RAISE NOTICE 'Total: %', total;
END;
AINTO cannot be used with EXECUTE
BUsing %L quotes the column name as a literal, causing an error
CRAISE NOTICE syntax is incorrect
DVariable col should be integer, not text
Step-by-Step Solution
Solution:
  1. Step 1: Understand format specifiers

    %L quotes the argument as a literal string, which is wrong for column names.
  2. Step 2: Correct usage

    %I should be used to safely insert identifiers like column names.
  3. Final Answer:

    Using %L quotes the column name as a literal, causing an error -> Option B
  4. Quick Check:

    Use %I for identifiers, not %L [OK]
Quick Trick: Use %I for identifiers in format(), not %L [OK]
Common Mistakes:
  • Using %L for column names
  • Thinking INTO is invalid with EXECUTE
  • Misunderstanding RAISE NOTICE syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes