7

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 Answer 1

7

It it necessary to return table

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

then

select * from withFunction()
Sign up to request clarification or add additional context in comments.

3 Comments

Okay. So there is no need to give EXECUTE or PERFORM statement before WITH?
@Meem execute is used for dynamic sql and perform when you don't want to return the result of the select
@ Clodoaldo Neto, Absolutely right. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.