Bird
0
0

In a PL/pgSQL function, you want to raise a notice with a variable and then raise an exception with a custom SQLSTATE code. Which snippet is correct?

hard📝 Application Q9 of 15
PostgreSQL - PL/pgSQL Fundamentals
In a PL/pgSQL function, you want to raise a notice with a variable and then raise an exception with a custom SQLSTATE code. Which snippet is correct?
ARAISE NOTICE 'Current value: %', val; RAISE EXCEPTION 'Custom error' WITH SQLSTATE 'P0001';
BRAISE NOTICE 'Current value: ' || val; RAISE EXCEPTION 'Custom error' SQLSTATE 'P0001';
CRAISE NOTICE('Current value: %', val); RAISE EXCEPTION 'Custom error' USING SQLSTATE = 'P0001';
DRAISE NOTICE 'Current value: %', val; RAISE EXCEPTION USING MESSAGE = 'Custom error', SQLSTATE = 'P0001';
Step-by-Step Solution
Solution:
  1. Step 1: Check RAISE NOTICE syntax

    RAISE NOTICE uses format string with % and variable as argument, as in RAISE NOTICE 'Current value: %', val; RAISE EXCEPTION USING MESSAGE = 'Custom error', SQLSTATE = 'P0001';.
  2. Step 2: Verify RAISE EXCEPTION with SQLSTATE

    Correct syntax uses RAISE EXCEPTION USING MESSAGE and SQLSTATE, as in RAISE NOTICE 'Current value: %', val; RAISE EXCEPTION USING MESSAGE = 'Custom error', SQLSTATE = 'P0001';.
  3. Step 3: Identify syntax errors in other options

    The other three options misuse concatenation, parentheses, or keywords.
  4. Final Answer:

    RAISE NOTICE 'Current value: %', val; RAISE EXCEPTION USING MESSAGE = 'Custom error', SQLSTATE = 'P0001'; -> Option D
  5. Quick Check:

    Use USING clause for custom SQLSTATE in RAISE EXCEPTION [OK]
Quick Trick: Use USING MESSAGE and SQLSTATE for custom exceptions [OK]
Common Mistakes:
  • Concatenating strings instead of using % placeholders
  • Incorrect syntax for SQLSTATE assignment
  • Omitting USING keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes