How to write WITH(CTE) within function in PostgreSQL

一曲冷凌霜 提交于 2020-02-04 08:58:30

问题


I am trying to use "WITH" that is Common Table Expression within the function of PostgreSQL.

Here is the following example:

Example:

 Create or replace function withFunction() returns void as
 $Body$
 Begin
  WITH cmn_l1
  AS
  (
    SELECT "PhoneNumber1","PhoneNumber2",
    DENSE_RANK() OVER(Partition by "PhoneNumber1" Order By "PhoneNumber2" )FoundIn
    From tablename;
  )
 SELECT DISTINCT * INTO temptable
 FROM cmn_l1
 WHERE FoundIn > 1;

 end;
 $Body$
 language plpgsql;

Question: How to execute and get values into the above table using WITH within function?


回答1:


It it necessary to return table

Create or replace function withFunction()
returns table(phone1 text, phone2 text) as

then

select * from withFunction()


来源:https://stackoverflow.com/questions/24205381/how-to-write-withcte-within-function-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!