postgresql - Converting the result of a dynamic query to json -


i'm trying convert dynamic query result json , return json result of function (this simplified version, where clause in actual code considerably longer).

create or replace function get_data_as_json(tbl regclass, p_version_id integer)   returns json $$ begin     return to_json( execute 'select * '|| tbl                          || ' version_id = p_budget_version_id' ); end; $$ language plpgsql; 

however, code results in type "execute" not exist error.
how run dynamic query, , convert result json?

if returning setof you'd need use return query execute construct, producing dynamic query returns want. since you're not, use regular execute ... into variable return.

untested, in vaguely right direction:

create or replace function get_data_as_json(tbl regclass, p_version_id integer) returns json $$ declare     my_result json; begin     execute format('select to_json(*) %i version_id = p_budget_version_id',tbl) my_result;     return my_result; end; $$ language plpgsql; 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -