Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres: can I prepare unnamed statement from SQL

https://www.postgresql.org/docs/current/static/libpq-exec.html next to PQprepare:

The function creates a prepared statement named stmtName from the query string, which must contain a single SQL command. stmtName can be "" to create an unnamed statement, in which case any pre-existing unnamed statement is automatically replaced; otherwise it is an error if the statement name is already defined in the current session.

and further:

Prepared statements for use with PQexecPrepared can also be created by executing SQL PREPARE statements.

now https://www.postgresql.org/docs/current/static/sql-prepare.html

PREPARE name [ ( data_type [, ...] ) ] AS statement

and:

name An arbitrary name given to this particular prepared statement. It must be unique within a single session and is subsequently used to execute or deallocate a previously prepared statement.

Question: Can I not create unnamed prepared statement using SQL statement PREPARE? silently reusable and all the rest...

t=# prepare ""(int) as select $1,now();
ERROR:  zero-length delimited identifier at or near """"
LINE 1: prepare ""(int) as select $1,now();

and ommiting the name:

ERROR:  syntax error at or near "as"

Or I'm missing something very obvious?..

like image 841
Vao Tsun Avatar asked Jun 19 '26 16:06

Vao Tsun


1 Answers

No. Unnamed prepared statements are only supported on the wire protocol.

They're mainly used for one-shot statements with bind parameters, and for batching.

like image 115
Craig Ringer Avatar answered Jun 24 '26 01:06

Craig Ringer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!