PL/pgSQL:can';t在'in'运算符的右侧使用变量

PL/pgSQL:can';t在'in'运算符的右侧使用变量,sql,postgresql,stored-procedures,plpgsql,Sql,Postgresql,Stored Procedures,Plpgsql,我正在使用PostgreSQL 10编写一个函数,但我不知道如何将使用的子查询提取到变量中。我的代码如下所示 create function "is_valid_slot_task"() returns trigger as $$ <<block>> declare schedule_id integer; ... begin block.schedule_id := (select distinct c."schedule_id"

我正在使用PostgreSQL 10编写一个函数,但我不知道如何将使用的子查询提取到变量中。我的代码如下所示

create function "is_valid_slot_task"() returns trigger as $$
<<block>>
declare
    schedule_id integer;
    ...
begin
    block.schedule_id := (select distinct c."schedule_id"
                          from "slot" as s
                          join "column" as c on c."column_id" = s."column_id"
                          where s."slot_id" = new."slot_id");
    if block.schedule_id
       in
       (select distinct c."schedule_id"
        from "slot_task" as st
        join "slot" as s on s."slot_id" = st."slot_id"
        join "column" as c on c."column_id" = s."column_id"
        where st."task_id" = new."task_id")
    ...
不同之处在于,我试图声明一个变量,我希望将子查询的结果分配给该变量(一个列表/数组/集合整数),然后在操作符中的
右侧使用该变量

最后一个版本不起作用,我得到
SQL错误[42601]:错误:在“block”处或附近出现语法错误…
。给你

我怎样才能做到这一点?我做错了什么?有解决办法吗


我的目标是在以后重用子查询的结果,以避免多次键入同一查询,并理解为什么我不能这样做。我还在声明中尝试了不同的数据类型:
any
%ROWTYPE
,但我就是不明白发生了什么。

变量
schedule\u id
是一个数组,您应该使用
array\u agg()
为它分配一个数组值。接下来,在
运算符中使用而不是

block.schedule_ids := (select array_agg(distinct c."schedule_id")
                       from "slot_task" as st
                       join "slot" as s on s."slot_id" = st."slot_id"
                       join "column" as c on c."column_id" = s."column_id"
                       where st."task_id" = new."task_id");
if block.schedule_id = any (block.schedule_ids)

@stickybit好吧,我的印象是PL/SQL是一种标准的东西,由PL/pgSQL实现,但我现在还不确定。所提供的解决方案包含特定于PostgreSQL的内容,但我很好奇,在一般意义上,在PL/SQL或PL/pgSQL中,可以使用什么样的语法来解决我的问题(我想我在PL/SQL中也可能遇到同样的问题)。
block.schedule_ids := (select array_agg(distinct c."schedule_id")
                       from "slot_task" as st
                       join "slot" as s on s."slot_id" = st."slot_id"
                       join "column" as c on c."column_id" = s."column_id"
                       where st."task_id" = new."task_id");
if block.schedule_id = any (block.schedule_ids)