PostgreSQL 9.3:从函数返回字符串

MAK*_*MAK 1 postgresql postgresql-9.3

我有以下函数调用为pro().从中我想通过联合所有两个select语句和产品输出返回字符串.

功能:亲()

我的尝试:

create or replace function pro()
returns varchar as

$$

declare
    sql varchar;
    q varchar;
begin
    sql := 'SELECT DISTINCT  CAST(COUNT(ProductNumber) as varchar) ||'' - Count of the product Number '' as Descp
        FROM product
        UNION ALL
        SELECT DISTINCT CAST(COUNT(ProductName) AS varchar) || '' - Count of the product Name '' as Descp
        FROM product';

    raise info '%',sql;

    execute sql into q;

    return q;

end;
$$

language plpgsql;
Run Code Online (Sandbox Code Playgroud)

调用功能:

select pro();
Run Code Online (Sandbox Code Playgroud)

这只返回select语句的第一部分:

 ______________________________________
|pro                                   |
|character varying                     |
|______________________________________|
|6 - Count of the product Number       |
|______________________________________|
Run Code Online (Sandbox Code Playgroud)

预期的结果应该是:

 ______________________________________
|pro                                   |
|character varying                     |
|______________________________________|
|6 - Count of the product Number       |
|______________________________________|
|6 - Count of the product Name         |
|______________________________________|
Run Code Online (Sandbox Code Playgroud)

Viv*_* S. 5

尝试使用这些功能:

运用 plpgsql

create or replace function pro1()returns 
table ( 
       descp text
      )
as
$$
begin
    return QUERY execute (
         'SELECT DISTINCT  CAST(COUNT(product) as varchar) ||'' - Count of the product Number '' as Descp
         FROM product
         UNION ALL
         SELECT DISTINCT CAST(COUNT(productid) AS varchar) || '' - Count of the product Name '' as Descp
         FROM product');
end;
$$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)

要么

运用 sql

create or replace function pro2() returns table  ( descp text) 
as
$$
  SELECT DISTINCT  CAST(COUNT(product) as varchar) ||' - Count of the product Number ' as Descp
  FROM product
    UNION ALL
  SELECT DISTINCT CAST(COUNT(productid) AS varchar) || ' - Count of the product Name 'as Descp
  FROM product;
$$
language sql;
Run Code Online (Sandbox Code Playgroud)