Re: Pass parameters to SQL script

From: Daniel Martini <dmartini(at)uni-hohenheim(dot)de>
To: Fuchs Clemens <clemens(dot)fuchs(at)siemens(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Pass parameters to SQL script
Date: 2004-08-19 10:00:01
Message-ID: 1092909601.41247a21119e8@webmail.uni-hohenheim.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

Citing Fuchs Clemens <clemens(dot)fuchs(at)siemens(dot)com>:
> is it possible to pass parameters to a SQL script, which I launch via the
> psql shell?

yes

> In Oracle it works like that:
>
> sqlplus myscript.sql myschema
>
> and within the script I can use the parameter like this:
>
> CONCAT .
> CREATE TABLE &1..test (name VARCHAR2(100));
>
> Is there a counterpart for PostgreSQL?

call psql like this to set a variable named your_variable_name to my_table:

psql -v your_variable_name=my_table

to expand the variable your_variable_name to its value (my_table in this case)
in the sql script, precede its name with a colon, like so:

select * from :your_variable_name;

which will expand to:

select * from my_table;

the psql manpage has more info on all this.

Regards,
Daniel

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Oliver Elphick 2004-08-19 10:04:18 Re: pg_dump feature request: Exclude tables?
Previous Message Andrew Hall 2004-08-19 09:53:09 Stored Procedures woes