Node-postgres: Can't parameterize an anonymous block?

Created on 3 Jul 2015  路  2Comments  路  Source: brianc/node-postgres

I have an anonymous block (Postgres' DO statement) which I supply a parameter to, but I receive this:

error: bind message supplies 1 parameters, but prepared statement "" requires 0

I am unable to determine if this is a limitation of Postgres itself or node-postgres, the anonymous block itself can't have parameters, but if this is being chucked into an anonymous prepared statement... Can anyone shed some light on this?

If it is possible, support would be really useful. I have a situation where doing several inserts one at a time in node would cause issues, and being able to send them along as a single monolithic block simplifies things greatly.

Most helpful comment

An anonymous DO block cannot have parameters. The error message you're getting is because you've supplied a parameter while executing it but the PostgreSQL server is complaining that the statement you executed does use them.

If your code is invoking an existing function you can invoke it by executing it in a regular SELECT like this: SELECT my_func($1, $2)

If the function doesn't exist, i.e. all the logic is in the DO block, then you can define it as a temporary function via CREATE TEMPORARY FUNCTION my_func ... and then execute it.

All 2 comments

An anonymous DO block cannot have parameters. The error message you're getting is because you've supplied a parameter while executing it but the PostgreSQL server is complaining that the statement you executed does use them.

If your code is invoking an existing function you can invoke it by executing it in a regular SELECT like this: SELECT my_func($1, $2)

If the function doesn't exist, i.e. all the logic is in the DO block, then you can define it as a temporary function via CREATE TEMPORARY FUNCTION my_func ... and then execute it.

Closing this - more of a postgres question than node-postgres me thinks & @sehrope did a bang up job on the answer! :clap:

Was this page helpful?
0 / 5 - 0 ratings