
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Dynamic SQL to get a parameter and use it in LIKE for a new table created inside a stored procedure
For this, use prepared statement. Let us first create a table −
mysql> create table DemoTable1973 ( StudentId int, StudentName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1973 values(101,'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1973 values(102,'John Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1973 values(103,'David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1973 values(104,'John Smith'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1973;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 101 | Chris | | 102 | John Doe | | 103 | David | | 104 | John Smith | +-----------+-------------+ 4 rows in set (0.00 sec)
Following is the query to create a stored procedure and a new table with LIKE clause having a value from procedure call −
mysql> DELIMITER // mysql> create PROCEDURE demo_create(IN newTableName varchar(20),IN tbl varchar(20)) BEGIN DROP TABLE IF EXISTS newTableName; SET @query= CONCAT('CREATE TABLE newTableName as SELECT * from DemoTable1973 WHERE StudentName like ''%',tbl,'%'''); PREPARE ps FROM @query; EXECUTE ps; END // Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ;
Call the stored procedure using call command −
mysql> call demo_create('newTableName','John'); Query OK, 2 rows affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from newTableName;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 102 | John Doe | | 104 | John Smith | +-----------+-------------+ 2 rows in set (0.00 sec)
- Related Articles
- Implement Dynamic SQL query inside a MySQL stored procedure?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Create a table inside a MySQL stored procedure and insert a record on calling the procedure
- Create a stored procedure to get the detail of a particular MySQL table stored in a database?
- Use IN() to get only a particular record in a MySQL stored procedure?
- What is STORED PROCEDURE in a DB2? How will you create a new stored procedure?
- MySQL Stored Procedure to create a table?
- MySQL stored-procedure: out parameter?
- Where is an object stored if it is created inside a block in C++?
- Difference between stored procedure and triggers in SQL
- How to use an OUT parameter / read data with SELECT from table in a MySQL procedure?
- How can I get all the records of a table by passing its name as the parameter of MySQL stored procedure?
- Insert data in a table in MySQL stored procedure?
- How to correctly use DELIMITER in a MySQL stored procedure?
- Display table records from a stored procedure in MySQL

Advertisements