Home > Database > Mysql Tutorial > How to Refactor a PL/pgSQL Function to Handle Dynamic SQL Return Types?

How to Refactor a PL/pgSQL Function to Handle Dynamic SQL Return Types?

Linda Hamilton
Release: 2025-01-22 23:32:11
Original
204 people have browsed it

How to Refactor a PL/pgSQL Function to Handle Dynamic SQL Return Types?

Refactor PL/pgSQL functions to return output of various SELECT queries

Dynamic SQL and return types

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.

Fixed return type solution

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>
Copy after login

The number of columns is variable, but the type is the same

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>
Copy after login

Complete table type

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>
Copy after login

How to use:

<code class="language-sql">SELECT * FROM data_of(NULL::pcdmet, 17);</code>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template