Wednesday, 25 July 2018

Prepared or Parameterized Statement in MySQL

I already shared a theory note on Prepared or Parameterized statements of Database System.
In this post, I am sharing a practical about prepared statements of MySQL.
You can write or create a prepared statement in MySQL.
But this is not an efficient way because the binary protocol through a prepared statement API is better.
But still, you can write, and even it doesn’t require any other programming, you can directly write in SQL.
You can use a prepared statement for MySQL Client program.
You can also use a prepared statement in a stored procedure for the dynamic SQL approach.
Below is a small practical demonstration on this:
Let’s first create sample table and data.
Create prepared statement:
You can see ‘?’ in WHERE clause. This ‘? ‘ means a required parameter.
You can pass the parameter during execution of Prepared Statement.
Execute Prepared Statement:
Result is:
De-allocate prepared statement:
You can also set max_prepared_stmt_count this is a system variable to guard creation of too many prepared statements.

0 comments:

Post a Comment