在Postgresql中控制IF上的3个条件

在Postgresql中控制IF上的3个条件,sql,postgresql,Sql,Postgresql,我需要控制从触发器传入的IF上的3个条件 如果三者之一为空。 Follow代码只控制第一个值(puntuacions),而不控制下一个值 create or replace function p_controla() returns trigger as $controla_puntuacions$ begin IF new.puntuacio or new.contingut or new.data IS NULL THEN RAISE 'ONE of the columns is empty

我需要控制从触发器传入的IF上的3个条件 如果三者之一为空。 Follow代码只控制第一个值(puntuacions),而不控制下一个值

create or replace function p_controla() returns trigger as $controla_puntuacions$
begin
IF new.puntuacio or new.contingut or new.data IS NULL
THEN
RAISE 'ONE of the columns is empty';
ELSE
RAISE ' ITS OK! ';
END IF;
END;
$controla_puntuacions$ language plpgsql;

create trigger controla_puntuacions
before insert on puntuacions
for each row execute procedure p_controla();

尝试测试连接它们,如果其中任何一个为null,则结果将为null:

create or replace function p_controla() returns trigger as $controla_puntuacions$
begin
IF (new.puntuacio::text || new.contingut::text || new.data::text) IS NULL
THEN
RAISE 'ONE of the columns is empty';
ELSE
RAISE ' ITS OK! ';
END IF;
END;
$controla_puntuacions$ language plpgsql;

尝试测试连接它们,如果其中任何一个为null,则结果将为null:

create or replace function p_controla() returns trigger as $controla_puntuacions$
begin
IF (new.puntuacio::text || new.contingut::text || new.data::text) IS NULL
THEN
RAISE 'ONE of the columns is empty';
ELSE
RAISE ' ITS OK! ';
END IF;
END;
$controla_puntuacions$ language plpgsql;

IS NULL的优先级高于或:

因此,您的IF语句的工作原理如下:

IF new.puntuacio or new.contingut or (new.data IS NULL)
正确的IF语句:

IF new.puntuacio IS NULL or new.contingut IS NULL or new.data IS NULL

IS NULL的优先级高于或:

因此,您的IF语句的工作原理如下:

IF new.puntuacio or new.contingut or (new.data IS NULL)
正确的IF语句:

IF new.puntuacio IS NULL or new.contingut IS NULL or new.data IS NULL

您可能需要查看上的文档。按照当前的编码,您已经有效地编码了“引发异常”,并且您的事务异常终止。此外,作为一个学习练习,触发这个可能是可以的,但更好的设计是将列定义为“NOTNULL”。这使得触发不必要。应尽可能避免使用触发器。您可能希望查看上的文档。按照当前的编码,您已经有效地编码了“引发异常”,并且您的事务异常终止。此外,作为一个学习练习,触发这个可能是可以的,但更好的设计是将列定义为“NOTNULL”。这使得触发不必要。只要有可能,就应该避免使用触发器。这很有效!谢谢你的工作!感谢取决于数据类型,此操作可能会失败。例如:选择NULL::int | | NULL::json@FrankHeikens的观点很好,因此根据数据类型将它们转换为文本,这可能会失败。例如:选择NULL::int | | NULL::json@FrankHeikens的观点很好,所以将其转换为文本