I'm trying to convert a dynamic query result into json
and then return that json
as the result of this function (this is a simplified version, the WHERE
clause in my actual code is considerably longer).
CREATE OR REPLACE FUNCTION get_data_as_json(tbl regclass, p_version_id integer)
RETURNS json AS $$
BEGIN
RETURN to_json( EXECUTE 'SELECT * FROM '|| tbl
|| ' WHERE version_id = p_budget_version_id' );
END;
$$ LANGUAGE plpgsql;
However, this code results in a type "execute" does not exist
error.
How do I run the dynamic query, and then convert the result to JSON?