MySQL - PREPARE Statement



MySQL PREPARE Statement

A prepared statement in MySQL represents a precompiled statement. A statement is compiled and stored in a prepared statement and you can later execute this multiple times. Instead of values we pass place holders to this statement.

If you want to execute several identical queries (that differ by values only). You can use prepared statements. You can execute these statements in client libraries as well as in SQL scripts.

A SQL prepared statement is based on three statements namely −

  • PREPARE
  • EXECUTE
  • DEALLOCATE PREPARE

The PREPARE statement/command among the above three prepares the statement. Once you prepare it you need to set values for the place holders using the SET statement.

Syntax

Following is the syntax of the PREPARE statement −

PREPARE stmt_name FROM preparable_stmt

Where stmt_name is the name of the prepared statement which you can refer later.

Example

Suppose we have created a table named Employee in the database using the CREATE statement and inserted three records in it as shown below −

CREATE TABLE Employee(
   Name VARCHAR(255), 
   Salary INT, 
   Location VARCHAR(255)
);

Following statements inserts records in the above created table −

INSERT INTO Employee VALUES 
('Amit', 6554, 'Hyderabad'),
('Sumith', 5981, 'Vishakhapatnam'),
('Sudha', 7887, 'Vijayawada');

If you observe the above statements except the values all the insert statements are identical. You can prepare a statement with place holders instead of values as −

PREPARE prepared_stmt FROM 'INSERT INTO EMPLOYE VALUES (?, ?, ?)';
Statement prepared

Once you prepare the statement you need to set values for the place holders as follows −

SET @name = 'Raghu';

SET @sal = 9878;

SET @loc = 'Delhi';

You can execute the above prepared statement using the EXECUTE statement −

EXECUTE prepared_stmt USING @name, @sal, @loc;

Verification

After executing the prepared statement if you verify the contents of the employee table you can observe the newly inserted row −

SELECT * FROM EMPLOYEE;

Output

Following is the output of the above query −

Name Salary Location
Amit 6554 Hyderabad
Sumith 5981 Vishakhapatnam
Sudha 7887 Vijayawada
Raghu 9878 Delhi

Example

Following is a prepared statement with the SELECT query −

--Preparing the statement
PREPARE prepared_stmt FROM 'SELECT ? FROM EMPLOYE where Name = ?';
Statement prepared

--Setting the values
SET @col = 'Salary';

SET @name = 'Raghu';
--Executing the statement
EXECUTE prepared_stmt USING @col, @name;

Output

The above query produces the following output −

?
Salary

Example

Assume we have created another table named Student using the Create statement shown below −

Create table Student(
   Name Varchar(35), 
   age INT, 
   Score INT
);

Now, let us try to insert some records into the Student table −

INSERT INTO student values 
('Jeevan', 22, 8),
('Raghav', 26, –3),
('Khaleel', 21, –9),
('Deva', 30, 9);

You can choose the table to execute a query dynamically using this statement as shown below −

--Setting the table name dynamically
SET @table = 'Student';
SET @statement = CONCAT('SELECT * FROM ', @table);

--Preparing the statement
PREPARE prepared_stmt FROM @statement;
Statement prepared

--Executing the statement
EXECUTE prepared_stmt;

Output

Following is the output of the above query −

Name age Score
Jeevan 22 8
Raghav 26 -3
Khaleel 21 -9
Deva 30 9
Advertisements