agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: aditya desai <admad123@gmail.com>
To: pgsql-sql <pgsql-sql@lists.postgresql.org>
Subject: Re: How can i pass variable in dblink_connect_u ?
Date: Tue, 1 Feb 2022 08:41:08 -0500
Message-ID: <cbda445a-31c2-847e-69de-b80c90c9e78e@joeconway.com> (raw)
In-Reply-To: <CAN0SRDEWatOY8T7+WL2-vkx1Qff=LjQ+biPSnT4JbnnmVtYvkA@mail.gmail.com>
References: <CAN0SRDEWatOY8T7+WL2-vkx1Qff=LjQ+biPSnT4JbnnmVtYvkA@mail.gmail.com>

On 2/1/22 05:42, aditya desai wrote:
> Hi All,
> how can I pass variables in dblink_connect_u ?
> 
> Current code hardcoded DB name:
> perform * from dblink_connect_u('myconn','dbname=mydb');
> 
> Expected:
> databasename=current_database();
> perform * from dblink_connect_u('myconn','dbname=databasename');
> 
> databasename is getting considered as text.

Correct, it is a string literal. So just create the string literal using 
string manipulation functionality -- e.g.:

SELECT
  dblink_connect_u('myconn','dbname=' ||
  current_database());
  dblink_connect_u
------------------
  OK
(1 row)

HTH,

Joe

-- 
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development





view thread (3+ messages)  latest in thread

Message-ID: <cbda445a-31c2-847e-69de-b80c90c9e78e@joeconway.com>
Permalink:  ../cbda445a-31c2-847e-69de-b80c90c9e78e@joeconway.com/
Also on:    postgresql.org/message-id/cbda445a-31c2-847e-69de-b80c90c9e78e@joeconway.com

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mail@joeconway.com, admad123@gmail.com, pgsql-sql@lists.postgresql.org
  Subject: Re: How can i pass variable in dblink_connect_u ?
  In-Reply-To: <cbda445a-31c2-847e-69de-b80c90c9e78e@joeconway.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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