Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 September Prepared or Parameterized Statement in MySQL

Prepared or Parameterized Statement in MySQL

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

I already shared a theory note on Prepared or Parameterized statements of Database System.

Prepared or Parameterized Statements in 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.

1
2
3
4
5
6
7
8
9
CREATE TABLE Test
(
TestNumber INTEGER
,TestName VARCHAR(50)
);
 
INSERT INTO Test
VALUES (1,'ABC'),(8,'EFG')
,(16,'PQR'),(26,'XYZ'),(41,'XVK');

Create prepared statement:

1
2
3
PREPARE TestStmt FROM
'SELECT * FROM Test
WHERE TestNumber=?';

You can see ‘?’ in WHERE clause. This ‘? ‘ means a required parameter.
You can pass the parameter during execution of Prepared Statement.

Execute Prepared Statement:

1
2
SET @a = 8;
EXECUTE TestStmt USING @a;

Result is:

1
2
3
---------
8 | 'EFG'
---------

De-allocate prepared statement:

1
DEALLOCATE PREPARE TestStmt;

You can also set max_prepared_stmt_count this is a system variable to guard creation of too many prepared statements.

Sep 1, 2015Anvesh Patel
List all Dates between two dates in SQL ServerHow to Insert if not exists in MySQL
Comments: 4
  1. Arman
    September 12, 2015 at 1:46 am

    Really very good article Anvesh,
    You are doing really very good.
    Explain in very easy language, I also read your theory on What is prepared statement.
    Thank you,

  2. Navkar
    September 13, 2015 at 2:07 am

    Nice Article !
    Thank You…

  3. mahi
    September 24, 2015 at 11:12 am

    Good Theory !

  4. Trilt
    January 20, 2016 at 4:54 pm

    Excellent blog you’ve got here.. It’s hard to find good quality writing like yours these days. I really appreciate people like you! Take care!!|

Anvesh Patel
Anvesh Patel

Database Engineer

September 1, 2015 MySQLAnvesh Patel, database, database research and development, dbrnd, max_prepared_stmt_count, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Designing, MySQL Database Programming, MySQL Error, MySQL Performance Tunning, MySQL Query, MySQL Tips and Tricks, prepared statement
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....