Re: BUG #4944: Problems with using \set variables as strings in select statements

Lists: pgsql-bugs
From: "Bruno Scovoli Santos" <bruno(dot)scovoli(at)gmail(dot)com>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #4944: Problems with using \set variables as strings in select statements
Date: 2009-07-27 13:50:48
Message-ID: 200907271350.n6RDomVD056020@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 4944
Logged by: Bruno Scovoli Santos
Email address: bruno(dot)scovoli(at)gmail(dot)com
PostgreSQL version: 8.3.7
Operating system: Ubuntu 9.04 Jaunty Jackalope
Description: Problems with using \set variables as strings in select
statements
Details:

I hava a table as follows:

euclidhardware=# select * from fabricantes;
id_fabricante | nome | distribuidor | descricao
---------------+-----------+----------------------+-----------
1 | Genius | Atera informática | -
2 | Logitech | Atera informática | -
3 | Epson | Kalunga | -
4 | HP | Kalunga | -
5 | Lexmark | Kalunga | -
6 | Gigabyte | IMICROS | -
7 | ASUS | IMICROS | -
8 | ABIT | IMICROS | -
9 | AMD | IMICROS | -
11 | Intel | Balao da informática | -
12 | Kingston | Balao da informática | -
13 | Corsair | IMICROS | -
14 | Smartdata | ShopInfo | -
299 | ashhj | 78hkj | 8jhkljk

When I do:

brunodb=# \set fab 11
brunodb=# select * from fabricantes where id_fabricante = :fab;
id_fabricante | nome | distribuidor | descricao
---------------+-------+----------------------+-----------
11 | Intel | Balao da informática | -

OK, I get the right result. But it's seems that there is a bug for this:

brunodb=# \set nom 'Intel'
euclidhardware=# select * from fabricantes where nome like :nom;
ERROR: column "intel" does not exist
LINE 1: select * from fabricantes where nome like Intel;

Please, I have tried exaustively many other ways to achieve this result, and
searched many foruns. I dont realize where is my error, I think that it is a
bug?

Thanks in advance,

Bruno


From: Rolf Jentsch <RJentsch(at)electronicpartner(dot)de>
To: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4944: Problems with using \set variables as strings in select statements
Date: 2009-07-27 14:31:19
Message-ID: 200907271631.19416.RJentsch@electronicpartner.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Hallo Bruno,

Am Montag, 27. Juli 2009 15:50 schrieb Bruno Scovoli Santos:

>...
> brunodb=# \set nom 'Intel'
> euclidhardware=# select * from fabricantes where nome like :nom;
> ERROR: column "intel" does not exist
> LINE 1: select * from fabricantes where nome like Intel;
>
> Please, I have tried exaustively many other ways to achieve this result,
> and searched many foruns. I dont realize where is my error, I think that it
> is a bug?
>
> Thanks in advance,
>
> Bruno

The syntax for \set with strings is a bit bizarre. You have to use either
\set nom '\'Intel\''
or
\set nom '''Intel'''

cu
Rolf Jentsch
Entwicklung Mitglieder-Systeme Dezentral

ElectronicPartner GmbH
Mündelheimer Weg 40
40472 Düsseldorf

phone: +49-(0)211-4156-0
fax: +49-(0)211-4156-6865
eMail: rjentsch(at)electronicpartner(dot)de

Sitz der Gesellschaft Düsseldorf
Amtsgericht - Registergericht Düsseldorf - HRB 4078
Geschäftsführer: Dr. Jörg Ehmer, Oliver Haubrich,
Dr. Sven-Olaf Krauß, Karl Trautmann


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Bruno Scovoli Santos" <bruno(dot)scovoli(at)gmail(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #4944: Problems with using \set variables as strings in select statements
Date: 2009-07-27 14:36:15
Message-ID: 10370.1248705375@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

"Bruno Scovoli Santos" <bruno(dot)scovoli(at)gmail(dot)com> writes:
> brunodb=# \set nom 'Intel'
> euclidhardware=# select * from fabricantes where nome like :nom;
> ERROR: column "intel" does not exist
> LINE 1: select * from fabricantes where nome like Intel;

If you want to put quotes in the value of a variable, you need
this
\set nom '''Intel'''
or this
\set nom '\'Intel\''
the same as you would to write a string literal containing quotes
in SQL.

regards, tom lane