Home > database >  How to resolve syntax error with dynamic PostgreSQL statement
How to resolve syntax error with dynamic PostgreSQL statement

Time:07-01

Suppose I have a table called my_table that has 3 columns

id   | name |  state
----- ------ --------
1020 | 'A ' | 'VA'   
----- ------ --------
1021 | 'B'  | 'VA'
----- ------ --------
1022 | 'C'  | 'NC'  

I am having an issue with a simple dynamic statement I am trying to run. I don't see anything wrong with this, do you?

 EXECUTE 'SELECT * FROM my_schema.my_table WHERE id = '|| 1021;

I am just running this standalone. It should execute. Instead, I get a ERROR: syntax error at or near "'SELECT....

CodePudding user response:

EXECUTE in plain SQL is used to run prepared statements. Straight quote from the docs:

EXECUTE is used to execute a previously prepared statement. Since prepared statements only exist for the duration of a session, the prepared statement must have been created by a PREPARE statement executed earlier in the current session.

You're probably mixing it up with pl/pgsql EXECUTE, which is totally different.

  • Related