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

时间:2014-06-13 12:32:22

标签: postgresql common-table-expression

我试图在PostgreSQL的功能中使用Common表达式的“WITH”。

以下是以下示例:

示例

 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;

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

1 个答案:

答案 0 :(得分:1)

必须返回table

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

然后

select * from withFunction()