Returning a single row

PL/pgSQL can be used to return a single row from a function; an example of this type is the factorial function.

Some developers refer to PL/pgSQL and SQL functions returning a single-row, single-column scalar variable as scalar functions.

The RETURN type can be base, composite, domain, pseudo type, or domain data type. The following function returns a JSON representation of a certain account:

-- in sql 
CREATE OR REPLACE FUNCTION car_portal_app.get_account_in_json (account_id INT) RETURNS JSON AS $$
SELECT row_to_json(account) FROM car_portal_app.account WHERE account_id = $1;
$$ LANGUAGE SQL;

--- in plpgsql
CREATE OR REPLACE FUNCTION car_portal_app.get_account_in_json1 (acc_id INT) RETURNS JSON AS $$
BEGIN
RETURN (SELECT row_to_json(account) FROM car_portal_app.account WHERE account_id = acc_id);
END;
$$ LANGUAGE plpgsql;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset