postgresql_16:5.3.5.2 Executing a Statement with Input Parameters
PostgreSQL 16 > Client Interfaces > ECPG — Embedded SQL in C > Dynamic SQL > Executing a Statement with Input Parameters
A more powerful way to execute arbitrary SQL statements is to prepare them once and execute the prepared statement as often as you like. It is also possible to prepare a generalized version of a statement and then execute specific versions of it by substituting parameters. When preparing the statement, write question marks where you want to substitute parameters later. For example: EXEC SQL BEGIN DECLARE SECTION; const char *stmt = "INSERT INTO test1 VALUES(?, ?);"; EXEC SQL END DECLARE SECTION; EXEC SQL PREPARE mystmt FROM :stmt; ... EXEC SQL EXECUTE mystmt USING 42, 'foobar';
When you don't need the prepared statement anymore, you should deallocate it: EXEC SQL DEALLOCATE PREPARE *name*;