Sv: Using psql variables in DO-blocks - Mailing list pgsql-general

From Andreas Joseph Krogh
Subject Sv: Using psql variables in DO-blocks
Date
Msg-id VisenaEmail.ae.b3ae5af6d6b5af5a.168523d524e@tc7-visena
Whole thread Raw
In response to Using psql variables in DO-blocks  (Andreas Joseph Krogh <andreas@visena.com>)
List pgsql-general
På tirsdag 15. januar 2019 kl. 16:51:09, skrev Andreas Joseph Krogh <andreas@visena.com>:
Hi all.
 
I'm trying to use a psql variable in a DO-block, but it fails:
 
[snip]
 
Seems I was a bit lazy, here's what works:
 
\set resource_group 'Ressurser'
\set quoted_resource_group '\'' :resource_group '\''

set myvars.quoted_resource_group to :quoted_resource_group;

DO $$
begin
    if not exists(SELECT * FROM onp_group WHERE groupname = current_setting('myvars.quoted_resource_group')) then
        raise notice 'Group % not found, creating it.', current_setting('myvars.quoted_resource_group');    end if;
end;
$$;
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
 
Attachment

pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Using psql variables in DO-blocks
Next
From: Ron
Date:
Subject: Re: Refining query statement