plpgsql过程中返回错误的concat_ws没有结果数据的目标



我是plpgsql的新手,现在我已经接近我想要做的事情:

create or replace function shop_apply_search_filters(
price_min integer default null, 
price_max integer default null, 
ecom_id  integer default null, 
cat_1 text default null, 
cat_2 text default null)
returns text as
$$
BEGIN
IF cat_1 = '' THEN
cat_1 = null;
END IF;
IF cat_2 = '' THEN
cat_2 = null;
END IF;
select concat_ws(
' and ',
'price < '      || price_min,
'price > '      || price_max,
'ecom_id = '    || ecom_id,
'category_1 = ' || cat_1,
'category_2 = ' || cat_2
) AS filters;
END;
$$
LANGUAGE PLPGSQL;

如果我称之为SELECT shop_apply_search_filters(10,null,null,'',''),我会出现以下错误:

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function shop_apply_search_filters(integer,integer,integer,text,text) line 11 at SQL statement
SQL state: 42601

我不确定我需要改变什么才能使其工作

您需要return结果

RETURN 
concat_ws(
' and ',
'price < '      || price_min,
'price > '      || price_max,
'ecom_id = '    || ecom_id,
'category_1 = ' || cat_1,
'category_2 = ' || cat_2
);

最新更新