PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 15, 2022

[FIXED] How to resolve syntax error with dynamic PostgreSQL statement

 December 15, 2022     dynamic, postgresql, syntax     No comments   

Issue

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....


Solution

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.



Answered By - cogitoergosum
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing