Consider the following PostgreSQL function:
CREATE OR REPLACE FUNCTION get_employee_info(emp_id INT, OUT emp_name TEXT, OUT emp_salary NUMERIC) AS $$ BEGIN SELECT name, salary INTO emp_name, emp_salary FROM employees WHERE id = emp_id; END; $$ LANGUAGE plpgsql;
What will be the result of SELECT * FROM get_employee_info(3); if the employee with id=3 is named 'Alice' with salary 75000?
CREATE OR REPLACE FUNCTION get_employee_info(emp_id INT, OUT emp_name TEXT, OUT emp_salary NUMERIC) AS $$ BEGIN SELECT name, salary INTO emp_name, emp_salary FROM employees WHERE id = emp_id; END; $$ LANGUAGE plpgsql;
OUT parameters in PostgreSQL functions return a row with named columns.
The function uses OUT parameters, so calling SELECT * FROM get_employee_info(3); returns a row with columns emp_name and emp_salary populated with the employee's data.
Choose the correct statement about OUT parameters versus RETURNS TABLE in PostgreSQL functions.
Think about how output columns are defined and returned in each method.
OUT parameters declare output variables that become columns in the result set implicitly. RETURNS TABLE explicitly declares the output columns and their types.
Identify the correct PostgreSQL function syntax that uses OUT parameters to return a user's first and last name.
Check the placement of INTO and the RETURNS clause for OUT parameters.
Option D correctly declares OUT parameters and uses SELECT ... INTO to assign values. Option D uses RETURNS TABLE, not OUT parameters. Option D incorrectly uses RETURNS RECORD with OUT parameters. Option D has incorrect INTO placement causing syntax error.
Given this function:
CREATE FUNCTION get_product_info(p_id INT, OUT p_name TEXT, OUT p_price NUMERIC) AS $$ BEGIN SELECT name, price INTO p_name FROM products WHERE id = p_id; END; $$ LANGUAGE plpgsql;
Why does calling SELECT * FROM get_product_info(10); raise an error?
Check if all OUT parameters are assigned values before function ends.
The SELECT INTO only assigns p_name but not p_price. Since p_price is an OUT parameter, it must be assigned a value or the function will raise an error due to uninitialized output.
You have a function with three OUT parameters that fetch data from the same table. Which approach optimizes performance best?
Think about minimizing the number of queries to the database.
Using a single SELECT ... INTO statement to assign all OUT parameters at once reduces the number of queries and improves performance.