我有以下函数称为pro()
。我想通过联合所有两个select
语句和产品输出来返回字符串。
功能:pro()
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;
调用函数:
select pro();
这只返回select语句的第一部分:
______________________________________
|pro |
|character varying |
|______________________________________|
|6 - Count of the product Number |
|______________________________________|
但预期的结果应为:
______________________________________
|pro |
|character varying |
|______________________________________|
|6 - Count of the product Number |
|______________________________________|
|6 - Count of the product Name |
|______________________________________|
答案 0 :(得分: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;
或
使用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;