Обсуждение: psql questions

Поиск
Список
Период
Сортировка

psql questions

От
"Walker, Jed S"
Дата:
I am trying to figure out how to set a variable in psql and use it and how
to echo information.

in Oracle I can do:
define jed = foo
prompt This is the value of jed
select sysdate, &jed from foo;

and get
This is the value of jed
24-MAR-05  foo

How can do those things in psql?  (I found the \set which seems to work, no
error, but I can't figure out how to use the variable in a psql session)

Thanks in advance,

        Jed S. Walker




Re: psql questions

От
John DeSoi
Дата:
On Mar 24, 2005, at 6:29 PM, Walker, Jed S wrote:

> How can do those things in psql?  (I found the \set which seems to
> work, no
> error, but I can't figure out how to use the variable in a psql
> session)


You prefix the variable with a colon to get the value. Here are a few
examples:

\set mystring '\'here is a string\''

select :mystring || ' test';
        ?column?
----------------------
  here is a string test
(1 row)


\set myint 37

select :myint + 10;
  ?column?
----------
        47
(1 row)


John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL


Re: psql questions

От
"Walker, Jed S"
Дата:
Hi John,

Thanks, I had tried the semi-colon, it's the unusual quoting syntax I was
missing.

\set '/'jed\''
select :jed;

Is that the only way you can quote it to make a string work?



-----Original Message-----
From: John DeSoi [mailto:desoi@pgedit.com]
Sent: Thursday, March 24, 2005 5:07 PM
To: Walker, Jed S
Cc: 'pgsql-novice@postgresql.org'
Subject: Re: [NOVICE] psql questions



On Mar 24, 2005, at 6:29 PM, Walker, Jed S wrote:

> How can do those things in psql?  (I found the \set which seems to
> work, no
> error, but I can't figure out how to use the variable in a psql
> session)


You prefix the variable with a colon to get the value. Here are a few
examples:

\set mystring '\'here is a string\''

select :mystring || ' test';
        ?column?
----------------------
  here is a string test
(1 row)


\set myint 37

select :myint + 10;
  ?column?
----------
        47
(1 row)


John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL

Re: psql questions

От
John DeSoi
Дата:
On Mar 25, 2005, at 10:23 AM, Walker, Jed S wrote:

> Is that the only way you can quote it to make a string work?

Unfortunately, I don't think there is another way. This is one of the
reasons I'm working on php preprocessor for my editor. Here is a
preview if you are interested in this approach:

http://pgedit.com/node/view/28


John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL