initialize and use variable in query - Mailing list pgsql-general

From Glenn Schultz
Subject initialize and use variable in query
Date
Msg-id CAE-4=KFP9T+RD4kNfu4jY2SHKJEEsSVsF9wUTFzUm01P1vJigw@mail.gmail.com
Whole thread Raw
Responses Re: initialize and use variable in query  (Ray O'Donnell <ray@rodonnell.ie>)
Re: initialize and use variable in query  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: initialize and use variable in query  (Олег Самойлов <splarv@ya.ru>)
List pgsql-general
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?

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 $$;
select 
fnmloan.loanseqnum
,fnmloan.currrpb
from
fnmloan
join
fnmloan_data
on
fnmloan_data.loanseqnum = fnmloan.loanseqnum
where
fnmloan.fctrdt = (select * from startdate)

limit 10

pgsql-general by date:

Previous
From: Mitar
Date:
Subject: Using placeholders when creating a materialized view
Next
From: Ray O'Donnell
Date:
Subject: Re: initialize and use variable in query