Currently, your function returns a text string representing the generated SELECT statement. To automate execution and return results, you can use dynamic SQL. However, the difficulty lies in the undefined return type. Functions need to define a return type in the RETURNS clause.
Assuming a fixed return type, containing a timestamp column named datahora
and two additional columns with different names and types, you can implement the following:
<code class="language-sql">CREATE OR REPLACE FUNCTION data_of(_id integer) RETURNS TABLE (datahora timestamp, col2 text, col3 text) LANGUAGE plpgsql AS $func$ DECLARE _sensors text := 'col1::text, col2::text'; -- 将每一列强制转换为文本类型 _type text := 'foo'; BEGIN RETURN QUERY EXECUTE ' SELECT datahora, ' || _sensors || ' FROM ' || quote_ident(_type) || ' WHERE id = ORDER BY datahora' USING _id; END $func$;</code>
If the return type has a variable number of columns, but all columns are of the same type (for example, double), use the array type:
<code class="language-sql">CREATE OR REPLACE FUNCTION data_of(_id integer) RETURNS TABLE (datahora timestamp, names text[], values float8[]) LANGUAGE plpgsql AS $func$ DECLARE _sensors text := 'col1, col2, col3'; -- 列名的简单列表 _type text := 'foo'; BEGIN RETURN QUERY EXECUTE format(' SELECT datahora , string_to_array() -- 作为名称 , ARRAY[%s] -- 作为值 FROM %s WHERE id = ORDER BY datahora' , _sensors, _type) USING _sensors, _id; END $func$;</code>
To return all columns of a table, use a polymorphic type:
<code class="language-sql">CREATE OR REPLACE FUNCTION data_of(_tbl_type anyelement, _id int) RETURNS SETOF anyelement LANGUAGE plpgsql AS $func$ BEGIN RETURN QUERY EXECUTE format(' SELECT * FROM %s -- pg_typeof 返回 regtype,自动引用 WHERE id = ORDER BY datahora' , pg_typeof(_tbl_type)) USING _id; END $func$;</code>
How to use:
<code class="language-sql">SELECT * FROM data_of(NULL::pcdmet, 17);</code>
The above is the detailed content of How to Refactor a PL/pgSQL Function to Handle Dynamic SQL Return Types?. For more information, please follow other related articles on the PHP Chinese website!