如何在 PostgreSQL 的函数内编写WITH(CTE)

Mee*_*eem 5 postgresql common-table-expression

我正在尝试使用“WITH”,它是 PostgreSQL 函数中的公共表表达式。

以下是示例:

例子

 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;
Run Code Online (Sandbox Code Playgroud)

问题:如何在函数内使用WITH执行并获取上表中的值?

Clo*_*eto 6

是否需要返回table

Create or replace function withFunction()
returns table(phone1 text, phone2 text) as
Run Code Online (Sandbox Code Playgroud)

然后

select * from withFunction()
Run Code Online (Sandbox Code Playgroud)