通用表达式:关系不存在。

3

我正在使用PostgreSQL版本10.3,与函数内的公共表达式一起工作。以下是该函数的代码:

CREATE OR REPLACE FUNCTION subscriptions.to_supply_patients(
    character varying,
    timestamp with time zone)
    RETURNS void
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
AS $BODY$

declare
aws_sub_pro alias for $1; -- health professional aws_sub 
tour_date alias for $2; -- tour date to manage patients covered accounts
number_cover int; -- the number of account to manage
sub_list integer[]; -- array list for subscribers covered
no_sub_list integer[];-- array list for no subscribers covered
date_tour timestamp with time zone;--tour date to manage patients covered accounts converted with time zone converted to 23:59:59 
begin
select subscriptions.convert_date_to_datetime((select date(tour_date)),'23:59:59','0 days') into date_tour; -- function to convert time to 23:59:59
select count(*) from subscriptions.cover where aws_sub = aws_sub_pro into number_cover; -- global number of patient to cover
if number_cover > 0 then 
   begin
        if tour_date >= current_timestamp then -- if tour date is later than today date
           begin
                with cover_list as (
                                   select id_cover from subscriptions.cover where aws_sub = aws_sub_pro and cover_duration >0 and is_covered_auto = true 
                                    -- we selectionned here the patients list to cover for health pro with aws_sub = aws_sub_pro
                                   ),
                     sub_cover_list as (
                                        select id_subs from subscriptions.subscribers_cover inner join cover_list on cover_list.id_cover = subscribers_cover.id_cover
                                        -- list of subscribers covered
                                       ),
                     no_sub_cover_list as (
                                           select id_free_trial from subscriptions.no_subscribers_cover inner join cover_list on cover_list.id_cover = no_subscribers_cover.id_cover   
                                            -- list of no subscribers covered
                                           )
-----------------------------------------------------------------------------------------------------------------------------

                    select array( select id_subs from subscriptions.subscribers_cover inner join cover_list on cover_list.id_cover = subscribers_cover.id_cover 
                                ) into sub_list; -- convert list of subscribers covered into array list
                     if array_upper(sub_list,1) <>0 then -- if array list is not empty
                        begin
                             for i in 1 .. array_upper (sub_list,1) loop -- for every one in this list
                                  if date_tour = (select sub_deadline_date from subscriptions.subscription where id_subs =sub_list[i] ) then -- if tour date is equals to 
                                     -- the deadline date 
                                     begin
                                          update subscriptions.subscription
                                          set
                                             sub_expiration_date = sub_expiration_date + interval'30 days', -- add 30 days to the exp date
                                             sub_deadline_date = sub_deadline_date + interval'30 days', -- add 30 date to deadline date
                                             sub_source = aws_sub_pro, -- supply source is no the professional
                                             is_sub_activ = false -- we turn off patients subscription
                                          where id_subs = (select id_subs from subscriptions.subscription where id_subs =sub_list[i] );
                                     end;
                                   end if;
                             end loop;
                        end;
                     end if;
--------------------------------------------------------------------------------------------------------------------------------                 

                     select array(select id_free_trial from subscriptions.no_subscribers_cover inner join cover_list on cover_list.id_cover = no_subscribers_cover.id_cover   
                                 ) into no_sub_list;
                     if array_upper(no_sub_list,1) <>0 then
                        begin
                             for i in 1 .. array_upper (no_sub_list,1) loop
                                  if date_tour = (select expiration_date from subscriptions.free_trial where id_free_trial =no_sub_list[i] and is_free_trial_activ = true ) then
                                     begin
                                          update subscriptions.free_trial
                                          set
                                             expiration_date = expiration_date + interval'30 days'

                                          where id_free_trial = (select id_free_trial from subscriptions.free_trial where id_free_trial =no_sub_list[i] );
                                     end;
                                  end if;
                             end loop;
                        end;
                    end if; 
           end;
        else
           raise 'tour date must be later than today''s date' using errcode='71'; 
        end if;
   end;
else
   raise notice 'your cover list is empty. you don''t have any patient to cover' using errcode='70';
end if;
end;

$BODY$;

ALTER FUNCTION subscriptions.to_supply_patients(character varying, timestamp with time zone)
    OWNER TO master_pgsql_hygeexv2;

当我运行此函数时,出现以下错误:

错误: ERREUR: 关系“cover_list”不存在 LINE 1: ...rom subscriptions.no_subscribers_cover inner join cover_list...

翻译:

(关系“cover_list”不存在)

我尝试在查询窗口中仅运行CTE,但是仍然收到相同的错误消息。
是否有什么我遗漏的内容?

看起来应该可以工作。这是标准的PostgreSQL吗?哪个版本?你能发布完整的SQL语句吗? - Laurenz Albe
我已经回答了你的问题。 - Jawhar Dahmoul
我知道你说过你只尝试了CTE,但在第二个循环之前的查询中肯定存在错误。它使用了CTE 'cover_list',但该CTE仅在第一个语句中可用。在这里:select array(select id_free_trial from subscriptions.no_subscribers_cover inner join cover_list - Łukasz Kamiński
好的发现 @ŁukaszKamiński !! - Jawhar Dahmoul
1个回答

0

CTE是SQL语句的一部分,无法在其外部任何地方看到。

因此,您只能在带有WITH子句的SELECT语句中使用cover_list

要么在第二个SELECT语句中重复WITH子句,要么重构代码,使您只需要一个查询。

另一种选择是使用临时表。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接