Plpgsql错误:RETURN不能在返回空的函数中有参数 [英] plpgsql Error: RETURN cannot have a parameter in function returning void
本文介绍了Plpgsql错误:RETURN不能在返回空的函数中有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DO
$BODY$
DECLARE
a date[]:= array(select distinct start_of_period from monthly_rankings where balance_type=2);
res int[] = '{}';
BEGIN
FOR i IN array_lower(a,1) .. array_upper(a,1)-1
LOOP
res:=array_append(res,'SELECT COUNT(user_id) from (select user_id from monthly_rankings where start_of_period=a[i] except select user_id from monthly_rankings where start_of_period=a[i+1]) as b');
i:=i+1;
END LOOP;
RETURN res;
$BODY$ language plpgsql
我收到错误:无法检索结果:错误:返回函数中不能有返回空的参数
第11行:返回资源;
我对这种过程语言还很陌生,不明白为什么函数会返回空。我确实将值赋给了变量,并且我声明为空-而不是空-数组。是否存在语法错误或更重要的推理错误?
推荐答案
1。)您根本不能从DO
语句RETURN
。您必须改为CREATE FUNCTION
。
2.)你不需要这些。使用此查询,速度将快一个数量级:
WITH x AS (
SELECT DISTINCT start_of_period
,rank() OVER (ORDER BY start_of_period) AS rn
FROM monthly_rankings
WHERE balance_type = 2
)
SELECT x.start_of_period, count(*) AS user_ct
FROM x
JOIN monthly_rankings m USING (start_of_period)
WHERE NOT EXISTS (
SELECT 1
FROM x x1
JOIN monthly_rankings m1 USING (start_of_period)
WHERE x1.rn = x.rn + 1
-- AND m1.balance_type = 2 -- only with matching criteria?
AND m1.user_id = m.user_id
)
-- AND balance_type = 2 -- all user_id from these dates?
GROUP BY x.start_of_period
ORDER BY x.start_of_period
这包括最后一个限定start_of_period
,您可能希望像在plpgsql代码中一样将其排除。
这篇关于Plpgsql错误:RETURN不能在返回空的函数中有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文