postgresql函数根据IF-ELSEIF条件更新每一行

postgresql函数根据IF-ELSEIF条件更新每一行,postgresql,stored-procedures,psql,Postgresql,Stored Procedures,Psql,我正在尝试创建一个postgresql函数,它将由cron作业每30分钟执行一次。此函数将执行if-else条件,并基于当前时间和其他列值更新列 我在IF语句的开头一直遇到语法问题,无法理解原因: ERROR: syntax error at or near "scheduled_active_start" LINE 7: IF scheduled_active_start <= executionTime AND schedule... ^ *

我正在尝试创建一个postgresql函数,它将由cron作业每30分钟执行一次。此函数将执行if-else条件,并基于当前时间和其他列值更新列

我在IF语句的开头一直遇到语法问题,无法理解原因:

ERROR:  syntax error at or near "scheduled_active_start"
LINE 7:      IF scheduled_active_start <= executionTime AND schedule...
                ^
********** Error **********

ERROR: syntax error at or near "scheduled_active_start"
SQL state: 42601
Character: 144
ERROR:scheduled\u active\u start处或附近出现语法错误
第7行:如果计划的\u活动的\u开始语句的语法清楚地保留在
UPDATE
SET
关键字之间,除了表定义之外,没有其他内容。SQL中根本没有
IF
语句(据我所知)。请改用语句:

UPDATE post SET
  is_active = CASE 
    WHEN scheduled_active_start <= executionTime AND scheduled_active_end > executionTime AND is_active != true THEN true
    WHEN scheduled_active_end <= executionTime AND is_active != false THEN false
  END
WHERE -- Filter to avoid updating whole table
  (scheduled_active_start <= executionTime AND scheduled_active_end > executionTime AND is_active != true) OR
  (scheduled_active_end <= executionTime AND is_active != false);
更新帖子集
是否激活=案例
当计划的\u活动\u开始执行时间并且处于活动状态时!=对,然后对
计划时\u活动\u结束
UPDATE post SET
  is_active = CASE 
    WHEN scheduled_active_start <= executionTime AND scheduled_active_end > executionTime AND is_active != true THEN true
    WHEN scheduled_active_end <= executionTime AND is_active != false THEN false
  END
WHERE -- Filter to avoid updating whole table
  (scheduled_active_start <= executionTime AND scheduled_active_end > executionTime AND is_active != true) OR
  (scheduled_active_end <= executionTime AND is_active != false);