Bird
0
0

Identify the issue in this function:

medium📝 Debug Q6 of 15
PostgreSQL - Advanced PL/pgSQL
Identify the issue in this function:
CREATE FUNCTION faulty_func() RETURNS TABLE(num INT) AS $$ BEGIN RETURN 5; END; $$ LANGUAGE plpgsql;
ARETURN must be replaced with RETURN QUERY or RETURN NEXT for table functions.
BThe RETURNS TABLE clause is invalid for scalar returns.
CThe function must use RETURNS SETOF INT instead of RETURNS TABLE.
DThe function should not have a BEGIN-END block.
Step-by-Step Solution
Solution:
  1. Step 1: Understand RETURN usage

    Functions returning TABLE must use RETURN QUERY or RETURN NEXT to return rows.
  2. Step 2: Analyze the function

    This function uses RETURN with a scalar value, which is invalid.
  3. Step 3: Correct approach

    Use RETURN QUERY SELECT 5; or RETURN NEXT 5; inside the function.
  4. Final Answer:

    RETURN must be replaced with RETURN QUERY or RETURN NEXT for table functions. -> Option A
  5. Quick Check:

    RETURN QUERY returns sets, RETURN returns scalar. [OK]
Quick Trick: Use RETURN QUERY or RETURN NEXT in table functions [OK]
Common Mistakes:
  • Using RETURN with scalar value in RETURNS TABLE functions
  • Confusing RETURNS TABLE with RETURNS SETOF
  • Omitting RETURN QUERY or RETURN NEXT

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes