Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用CTE减少postgres plpgsql函数中的重复_Sql_Postgresql_Plpgsql_Common Table Expression - Fatal编程技术网

使用CTE减少postgres plpgsql函数中的重复

使用CTE减少postgres plpgsql函数中的重复,sql,postgresql,plpgsql,common-table-expression,Sql,Postgresql,Plpgsql,Common Table Expression,在SQL函数中,如果返回布尔值,则可以返回布尔值 with myquery as (delete from mytable where id = 'value1' returning 1) select exists (select * from another_function('value2') where (select count(*) from myquery) > 0); 但在plpgsql函数中,它不起作用,并给出错误查询,结果数据没有目标 在这个函数中,如果从mytabl

在SQL函数中,如果返回布尔值,则可以返回布尔值

with myquery as (delete from mytable where id = 'value1' returning 1)
select exists (select * from another_function('value2') where (select count(*) from myquery) > 0);
但在plpgsql函数中,它不起作用,并给出错误查询,结果数据没有目标

在这个函数中,如果从mytable中实际删除了任何行,我希望执行另一个_函数。问题是我重复了整个select exists部分,因为我在多个函数中使用它

是否有可能将更多的逻辑转移到另一个函数中?这样我就可以做这样的事

with myquery as (delete from mytable where id = 'value1' returning 1)
select * from another_function('value2', myquery)

如何将CTE传递到函数中,以便每次调用另一个函数时不必重复select exists和where select count*from myquery>0?

我希望辅助函数采用参数,例如从delete返回的id。然后看起来像:

with d as (
      delete from mytable
      where id = 'value1'
      returning id
     )
select another_function('value2', d.id)
from d;
这一次操作一个值,但这通常是人们想要的。如果希望同时传入所有ID,可以使用数组:

with d as (
      delete from mytable
      where id = 'value1'
      returning id
     )
select another_function('value2', array_agg(d.id))
from d;

我只是感到困惑。另一个函数在一种情况下接受一个参数,在另两种情况下,第二个参数是。什么CTE?元组?我想你应该问一个新问题,更详细地说明你想要完成什么。谢谢,你知道我如何让问题中的第一个查询不抛出查询没有结果数据错误的目标,当它被定义为返回布尔值时?它在使用sql函数而不是plpgsql时工作。@user779159。如果这是在函数中,则需要将值存储在某个位置。关于查询没有结果数据错误的目标,如果它是返回布尔值的常规sql函数,则我的第一个查询可以工作。但如果我将其更改为plpgsql函数,则会出现错误。通常,我可以通过执行类似于返回select exists的操作,从plpgsql函数返回布尔值。。。。。。。。为什么这在这里不可能?如果我在一个包含数据修改语句的return get with子句中使用with包装整个语句,那么它必须位于顶层。