I’m trying to create a function to insert data into a table. The query I’m using won’t be changing except for the schema names need to be variables. For instance, one of my schema names is bscu.members and then there’s 35 others which are very similar (wea.members, pcu.members.. etc etc..). I couldn’t find any help on how to create a function in Postgresql using variables.
this is what i came up with so far but it isn’t working
create or replace function attsummary(varchar)
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
insert into dwh.attribution_summary
select
m.source,
m.name,
m.partner_id,
d.ucic,
b.acct_type_desc as acct_desc,
a.begin_mo_balance as opening_balance,
c.date,
h.campaignname,
g.description as banner_desc,
f.create_time::timestamp as time_served,
'd' as dep_or_loan,
'h' as home_or_nonhome
from
$1.fact_deposits a
join $1.dim_acct_type b on a.acct_type_id = b.acct_type_id
join $1.dim_date c on a.date_id = c.date_id
join $1.dim_members d on a.ucic = d.ucic
join ad_delivery.sgmt_adic e on d.adic::varchar = e.adic
join ad_delivery.sgmt_user_tracker f on e.cookie_id = f.id
join ad_delivery.ox_banners g on g.bannerid = f.banner_id
join ad_delivery.ox_campaigns h on h.campaignid = f.campaign_id
join ad_delivery.sgmt_kli_adic i on e.adic = i.adic
join dwh.sgmt_clients m on m.partner_id = i.sgmt_partner_id
where
i.kli=8616208
and m.partner_id::integer != 909909
and then my select statement comes after.. I am using $1 for my variable where the schema name usually goes.
It must be dynamically generated