尝试执行FOREACH时出现语法错误

时间:2019-12-16 12:42:43

标签: sql postgresql function sql-update plpgsql

我具有以下功能:

ERROR:  ERROR:  syntax error(near: "END")
LINE 21:             END IF;

但是当我尝试运行它时,我得到了:

;

有人可以解释为什么会发生此错误吗?我没有看到语法问题。

我使用的资源:
https://dba.stackexchange.com/questions/234657/syntax-error-when-using-exception-inside-for-loop
https://www.postgresql.org/docs/9.4/plpgsql-control-structures.html

问题是THEN分支中的声明后缺少IF (category_index::int = 6 OR category_index::int = 7) THEN has_extra1_content = TRUE; /* semicolon here too! */ END IF; ,因此应该是:

UPDATE

根据您的建议,亲爱的stackoverflow用户,我已将此UPDATE campaign_campaign SET category = CASE WHEN ( campaign_campaign.category != '' AND regexp_split_to_array(campaign_campaign.category, ',')::int[] && ARRAY[6,7] AND NOT regexp_split_to_array(campaign_campaign.category, ',')::int[] && ARRAY[0,1,2,3,4,5] ) THEN 0 WHEN ( campaign_campaign.category != '' AND regexp_split_to_array(campaign_campaign.category, ',')::int[] && ARRAY[0,1,2,3,4,5] AND NOT regexp_split_to_array(campaign_campaign.category, ',')::int[] && ARRAY[6,7] ) THEN 1 ELSE 2 END; 语句替换为该函数:

type Student = { type: "student" } & CommonPerson
type Teacher = { type: "teacher" } & CommonPerson
type Other = { type: "other" } & CommonPerson
type CommonPerson = { name: string }
type Person = Student | Teacher | Other

const handleStudAndTeacher = (p: Student | Teacher) => {
    if (p.type === "student") {
        p // Student
    }
}

const handleAllPersons = (p: Person) => {
    if (p.type === "other") {
        p // Other
    }
}

handleStudAndTeacher(obj[0])
handleStudAndTeacher(obj[1])
handleStudAndTeacher({ type: "other", name: "Lui" }) // error (OK)
handleAllPersons({ type: "other", name: "Lui" })

1 个答案:

答案 0 :(得分:1)

看起来您可以将所有内容替换为:

int[]

这假定原始文本字符串中没有无关紧要的空格。否则,仍然有必要像以前一样强制转换为UPDATE

SET campaign_campaign.category = ...语句中的目标列不能被表限定,顺便说一句, category 不能。

然后应将列integer转换为true
或在false列中使用null / boolean / {{1}}。