Re: initialize and use variable in query

From: Ray O'Donnell <ray(at)rodonnell(dot)ie>
To: Glenn Schultz <glenn(at)bondlab(dot)io>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: initialize and use variable in query
Date: 2018-12-29 15:47:39
Message-ID: e8d1a269-24c2-2d05-33a7-c3ed1e925aae@rodonnell.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 29/12/2018 15:40, Glenn Schultz wrote:
> All,
>
> I need to initialize a variable and then use it in query.
> Ultimately this will part of a recursive CTE but for now I just need to
> work this out.  I followed the docs and thought I needed something like
> this.  But does not work-maybe I have misunderstood.  Is this possible?

Hi there,

What does "does not work" mean? What error do you get?

>
> SET max_parallel_workers_per_gather = 8;
> SET random_page_cost = 1;
> SET enable_partitionwise_aggregate = on;
> Do $$
> Declare startdate date;
> BEGIN
> startdate := (select max(fctrdt) from fnmloan);
> END $$;

A couple of things off the top of my head:

(i) I think you need "language plpgsql" (or whatever) after the DO block.

(ii) That assignment in the DO should probably be:

select max(fctrdt) into startdate from fnmloan;

I hope this helps.

Ray.

--
Raymond O'Donnell // Galway // Ireland
ray(at)rodonnell(dot)ie

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2018-12-29 15:59:32 Re: initialize and use variable in query
Previous Message Glenn Schultz 2018-12-29 15:40:34 initialize and use variable in query