Which of the following is the correct way to declare a PostgreSQL function that returns a set of text values?
easy📝 Syntax Q3 of 15
PostgreSQL - Advanced PL/pgSQL
Which of the following is the correct way to declare a PostgreSQL function that returns a set of text values?
CREATE FUNCTION get_words() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT ...; END; $$ LANGUAGE plpgsql;
ACREATE FUNCTION get_words() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT word FROM dictionary; END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION get_words() RETURNS text AS $$ BEGIN RETURN QUERY SELECT word FROM dictionary; END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION get_words() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT word FROM dictionary; END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION get_words() RETURNS SETOF text AS $$ BEGIN RETURN NEXT word FROM dictionary; END; $$ LANGUAGE plpgsql;
Step-by-Step Solution
Solution:
Step 1: Check RETURNS clause
Function must declare RETURNS SETOF text to return multiple text rows.
Step 2: Validate RETURN QUERY usage
RETURN QUERY with SELECT is correct to return multiple rows.
Step 3: Analyze options
CREATE FUNCTION get_words() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT word FROM dictionary; END; $$ LANGUAGE plpgsql; correctly uses RETURNS SETOF text and RETURN QUERY SELECT.
Final Answer:
Option A -> Option A
Quick Check:
RETURNS SETOF text with RETURN QUERY SELECT is correct [OK]
Quick Trick:Use RETURNS SETOF text with RETURN QUERY SELECT [OK]
Common Mistakes:
Omitting SETOF in RETURNS clause
Using wrong return type like integer
Using RETURN NEXT incorrectly with SELECT
Master "Advanced PL/pgSQL" in PostgreSQL
9 interactive learning modes - each teaches the same concept differently