pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Using psql variables in DO-blocks
2+ messages / 2 participants
[nested] [flat]

* Using psql variables in DO-blocks
@ 2019-01-15 15:51 Andreas Joseph Krogh <andreas@visena.com>
  2019-01-15 15:56 ` Re: Using psql variables in DO-blocks David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Andreas Joseph Krogh @ 2019-01-15 15:51 UTC (permalink / raw)
  To: pgsql-general

Hi all.
 
I'm trying to use a psql variable in a DO-block, but it fails:
 
\set resource_group 'Ressurser' \set quoted_resource_group '\'' 
:resource_group '\'' DO $$ begin  if not exists(SELECT * FROM tbl_group WHERE 
groupname = :quoted_resource_group)then  raise notice 'Group % not found, 
creating it.', :quoted_resource_group; end if; end; $$; 
 
ERROR:  syntax error at or near ":" 
 LINE 3: ... exists(SELECT * FROM tbl_group WHERE groupname = :quoted_re...
                                                            ^
  
Any hints?
 
-- Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
 <https://www.visena.com;

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Using psql variables in DO-blocks
  2019-01-15 15:51 Using psql variables in DO-blocks Andreas Joseph Krogh <andreas@visena.com>
@ 2019-01-15 15:56 ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: David G. Johnston @ 2019-01-15 15:56 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreas@visena.com>; +Cc: pgsql-general

On Tue, Jan 15, 2019 at 8:48 AM Andreas Joseph Krogh <andreas@visena.com>
wrote:

> Hi all.
>
> I'm trying to use a psql variable in a DO-block, but it fails:
>
 [...]

> Any hints?
>

Don't do that.  The body of a DO block is a string literal and psql won't
and shouldn't mess with its contents.  You'll need to use an actual
function and pass in the psql variable data via an input parameter.

set_config(...)/current_setting(...) is another option to consider.

David J.

Attachments:

noname (image/png, 1.9K) download view image
^ permalink raw reply [nested|flat] 2+ messages in thread


end of thread, other threads:[~2019-01-15 15:56 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-01-15 15:51 Using psql variables in DO-blocks Andreas Joseph Krogh <andreas@visena.com>
2019-01-15 15:56 ` David G. Johnston <david.g.johnston@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox