How to write such a query?

Started by Igor Korotover 4 years ago10 messagesgeneral
Jump to latest
#1Igor Korot
ikorot01@gmail.com

Hi, ALL,
In SQLite you can write:

SELECT a, b, c FROM foo WHERE id = :id;

where ":id" is the named parameter.

The query above is similar to

SELECT a,b,c FROM foo WHERE id = ?;

except that the parameter has a name.

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

Thank you.

#2Amul Sul
sulamul@gmail.com
In reply to: Igor Korot (#1)
Re: How to write such a query?

See prepare statement : https://www.postgresql.org/docs/current/sql-prepare.html

Show quoted text

On Thu, Jan 6, 2022 at 12:10 PM Igor Korot <ikorot01@gmail.com> wrote:

Hi, ALL,
In SQLite you can write:

SELECT a, b, c FROM foo WHERE id = :id;

where ":id" is the named parameter.

The query above is similar to

SELECT a,b,c FROM foo WHERE id = ?;

except that the parameter has a name.

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

Thank you.

#3David G. Johnston
david.g.johnston@gmail.com
In reply to: Igor Korot (#1)
Re: How to write such a query?

On Wednesday, January 5, 2022, Igor Korot <ikorot01@gmail.com> wrote:

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

The server, and its prepared SQL capability, doesn’t understand named
parameters. Only numbered ones.

That said, there are a number of different ways to write and execute SQL
available to you and each of those provides different extended
capabilities. For instance, psql and its variables feature.

David J.

#4Ron
ronljohnsonjr@gmail.com
In reply to: Igor Korot (#1)
Re: How to write such a query?

On 1/6/22 12:39 AM, Igor Korot wrote:

Hi, ALL,
In SQLite you can write:

SELECT a, b, c FROM foo WHERE id = :id;

where ":id" is the named parameter.

The query above is similar to

SELECT a,b,c FROM foo WHERE id = ?;

except that the parameter has a name.

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

Absolutely.  Of course, the exact method depends on the client.  Are you
referring to psql?

If so, then here's an example:
$ psql12 -v S=456789012 test
psql (12.8 (Ubuntu 12.8-1.pgdg18.04+1))
Type "help" for help.

test=# select * from employee where ssn = :'S';
    ssn    |      name       |  ssn_int
-----------+-----------------+-----------
 456789012 | Fred Flintstone | 456789012
(1 row)

In this case, column ssn is of type varchar(9).

--
Angular momentum makes the world go 'round.

#5Igor Korot
ikorot01@gmail.com
In reply to: Ron (#4)
Re: How to write such a query?

Hi, Ron,

On Thu, Jan 6, 2022 at 1:01 AM Ron <ronljohnsonjr@gmail.com> wrote:

On 1/6/22 12:39 AM, Igor Korot wrote:

Hi, ALL,
In SQLite you can write:

SELECT a, b, c FROM foo WHERE id = :id;

where ":id" is the named parameter.

The query above is similar to

SELECT a,b,c FROM foo WHERE id = ?;

except that the parameter has a name.

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

Absolutely. Of course, the exact method depends on the client. Are you
referring to psql?

If so, then here's an example:
$ psql12 -v S=456789012 test
psql (12.8 (Ubuntu 12.8-1.pgdg18.04+1))
Type "help" for help.

test=# select * from employee where ssn = :'S';
ssn | name | ssn_int
-----------+-----------------+-----------
456789012 | Fred Flintstone | 456789012
(1 row)

In this case, column ssn is of type varchar(9).

Is the syntax available since 9.0+? Or later?
I'm writing C++.

Thank you.

Show quoted text

--
Angular momentum makes the world go 'round.

#6Igor Korot
ikorot01@gmail.com
In reply to: David G. Johnston (#3)
Re: How to write such a query?

Hi, David,

On Thu, Jan 6, 2022 at 1:00 AM David G. Johnston
<david.g.johnston@gmail.com> wrote:

On Wednesday, January 5, 2022, Igor Korot <ikorot01@gmail.com> wrote:

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

The server, and its prepared SQL capability, doesn’t understand named parameters. Only numbered ones.

That said, there are a number of different ways to write and execute SQL available to you and each of those provides different extended capabilities. For instance, psql and its variables feature.

It looks like your answer contradicts with Ron...

Thank you.

Show quoted text

David J.

#7Ron
ronljohnsonjr@gmail.com
In reply to: Igor Korot (#6)
Re: How to write such a query?

On 1/6/22 1:07 AM, Igor Korot wrote:

Hi, David,

On Thu, Jan 6, 2022 at 1:00 AM David G. Johnston
<david.g.johnston@gmail.com> wrote:

On Wednesday, January 5, 2022, Igor Korot <ikorot01@gmail.com> wrote:

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

The server, and its prepared SQL capability, doesn’t understand named parameters. Only numbered ones.

That said, there are a number of different ways to write and execute SQL available to you and each of those provides different extended capabilities. For instance, psql and its variables feature.

It looks like your answer contradicts with Ron...

TIMTOWTDI

--
Angular momentum makes the world go 'round.

#8Igor Korot
ikorot01@gmail.com
In reply to: Amul Sul (#2)
Re: How to write such a query?

Hi, Armul,

On Thu, Jan 6, 2022 at 12:46 AM Amul Sul <sulamul@gmail.com> wrote:

See prepare statement : https://www.postgresql.org/docs/current/sql-prepare.html

The documentation is talking about a way to do it like:

SELECT a, b, c FROM foo WHERE id = $1,

which is equivalent to the

SELECT a, b, c FROM foo WHERE id = ?;

i.e. using unnamed parameter.

Thank you.

Show quoted text

On Thu, Jan 6, 2022 at 12:10 PM Igor Korot <ikorot01@gmail.com> wrote:

Hi, ALL,
In SQLite you can write:

SELECT a, b, c FROM foo WHERE id = :id;

where ":id" is the named parameter.

The query above is similar to

SELECT a,b,c FROM foo WHERE id = ?;

except that the parameter has a name.

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

Thank you.

#9Ron
ronljohnsonjr@gmail.com
In reply to: Igor Korot (#5)
Re: How to write such a query?

On 1/6/22 1:06 AM, Igor Korot wrote:

Hi, Ron,

On Thu, Jan 6, 2022 at 1:01 AM Ron <ronljohnsonjr@gmail.com> wrote:

On 1/6/22 12:39 AM, Igor Korot wrote:

Hi, ALL,
In SQLite you can write:

SELECT a, b, c FROM foo WHERE id = :id;

where ":id" is the named parameter.

The query above is similar to

SELECT a,b,c FROM foo WHERE id = ?;

except that the parameter has a name.

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

Absolutely. Of course, the exact method depends on the client. Are you
referring to psql?

If so, then here's an example:
$ psql12 -v S=456789012 test
psql (12.8 (Ubuntu 12.8-1.pgdg18.04+1))
Type "help" for help.

test=# select * from employee where ssn = :'S';
ssn | name | ssn_int
-----------+-----------------+-----------
456789012 | Fred Flintstone | 456789012
(1 row)

In this case, column ssn is of type varchar(9).

Is the syntax available since 9.0+? Or later?
I'm writing C++.

Like I said before... "the exact method depends on the client".  The C++
client */library/* is not the same as the psql *application*.

IOW, read the C+ client library documentation.

--
Angular momentum makes the world go 'round.

#10Dmitriy Igrishin
dmitigr@gmail.com
In reply to: Igor Korot (#1)
Re: How to write such a query?

On Thu, Jan 6, 2022, 09:40 Igor Korot <ikorot01@gmail.com> wrote:

Hi, ALL,
In SQLite you can write:

SELECT a, b, c FROM foo WHERE id = :id;

where ":id" is the named parameter.

The query above is similar to

SELECT a,b,c FROM foo WHERE id = ?;

except that the parameter has a name.

Is there a way to write such a SELECT statement with the
named parameter in PostgreSQL?

Named parameters of prepared statements are implemented in my C++ library
Pgfe.