This article is half-done without your Comment! *** Please share your thoughts via Comment ***
The PostgreSQL Prepared Statement or Parameterized Statements are always good for specific purpose like: can create Prepared Statement for frequently executing query of a session, It also prevents from SQL Injections.
Prepared Statements are faster for a particular session because It does not require parsing and compiling for each execution. It is only session specific and once a session kill, Prepared Statements automatically destroy.
Here, You can access more on Prepared Statements.
Small demonstration of PostgreSQL Prepared Statement:
Create a table with sample records:
1 2 3 4 5 6 7 8 9 10 11 |
CREATE TABLE tbl_Students ( StudID INTEGER PRIMARY KEY ,StudName CHARACTER VARYING ,StudClass CHAR(1) ); INSERT INTO tbl_Students VALUES (1,'Anvesh','A') ,(2,'Neevan','B'),(3,'Jenny','C') ,(4,'Roy','C'),(5,'Martin','C'); |
Create a Prepared Statement to SELECT Students data:
1 2 |
PREPARE pre_GetStudents (INT) AS SELECT *FROM tbl_Students WHERE StudID = $1; |
Execute a Prepared Statement:
1 |
EXECUTE pre_GetStudents(2); |
System view to check the Prepare statements of session:
1 |
SELECT *FROM pg_prepared_statements; |