比较块在 plpgsql 上不起作用。比较文本

问题描述 投票:0回答:1

我有以下 plpgsql 函数:

CREATE or replace TRIGGER conteo_nulls
AFTER INSERT OR UPDATE ON public.datos
   FOR EACH ROW EXECUTE FUNCTION conteo_nulls();





create or replace function conteo_nulls() returns trigger as $$
declare
c INT;
rescrape bool := false;
feriado_ bool := false;
dia text;
begin
    SELECT count(*) into c FROM public.datos where fundseries is null and scrap_date=new.scrap_date group by scrap_date;
    if c > 5 then
        rescrape := true;
    end if;
    dia := TO_CHAR(new.scrap_date, 'Day');
    RAISE NOTICE 'El día de la semana es: %', pg_typeof(TO_CHAR(new.scrap_date::DATE, 'Day'));
    **---THIS IF BLOCK NOT ENTER.
    if dia::TEXT = 'Sunday' or dia::TEXT = 'Saturday'::TEXT then
        RAISE NOTICE '****estoy dentro del if variable feriado es : %', feriado;
        feriado_ := true;
    end if;
    --END BLOCK
    **
    RAISE NOTICE 'estoy fuera del if variable feriado es : %', feriado_;
    insert into datos_resume values(new.scrap_date,c,rescrape,dia,feriado_);
RETURN NEW;
end;
$$
LANGUAGE plpgsql;

但是在第二个 IF 块上,它没有进入。两个文本都是“星期日”,但比较不起作用。它正在比较“Sunday”=“Sunday”,但它不会进入 if 块内部,也不会从 false 切换为 true。 你能帮帮我我做错了什么吗? 预先感谢。

postgresql triggers plpgsql postgresql-16
1个回答
0
投票

您的问题是

to_char(<date>, 'Day')
返回 9 个字符的固定长度值,根据需要在右侧空白填充。您可以填充目标值或对
to_char()
的结果进行 trin 运算。 (参见演示

所以类似:

create or replace function conteo_nulls() returns trigger as $$
declare
c INT;
rescrape bool := false;
feriado_ bool := false;
dia text;
begin
    SELECT count(*) into c FROM public.datos where fundseries is null and scrap_date=new.scrap_date group by scrap_date;
    if c > 5 then
        rescrape := true;
    end if;
    dia := trim(TO_CHAR(new.scrap_date, 'Day'));    --<< Changed here >> 
    RAISE NOTICE 'El día de la semana es: %', pg_typeof(TO_CHAR(new.scrap_date::DATE, 'Day'));
     
    if dia::TEXT = 'Sunday' or dia::TEXT = 'Saturday'::TEXT then
        RAISE NOTICE '****estoy dentro del if variable feriado es : %', feriado;
        feriado_ := true;
    end if;
    
    RAISE NOTICE 'estoy fuera del if variable feriado es : %', feriado_;
    insert into datos_resume values(new.scrap_date,c,rescrape,dia,feriado_);
RETURN NEW;
end;
$$
LANGUAGE plpgsql;
© www.soinside.com 2019 - 2024. All rights reserved.