
- 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
Set user-defined variable with table name in MySQL prepare statement?
Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(20) ); Query OK, 0 rows affected (0.71 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentName) values('Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(StudentName) values('David'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(StudentName) values('Sam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentName) values('Mike'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 2 | David | | 3 | Sam | | 4 | Mike | +-----------+-------------+ 4 rows in set (0.00 sec)
Here is the how you can use a user-defined variable to set a table name and work with MySQL prepare statement −
mysql> set @tableName = 'DemoTable'; Query OK, 0 rows affected (0.04 sec) mysql> set @queryToSelectStudentName := concat('select StudentName from ',@tableName); Query OK, 0 rows affected (0.28 sec) mysql> prepare executeQuery from @queryToSelectStudentName; Query OK, 0 rows affected (0.04 sec) Statement prepared mysql> execute executeQuery;
This will produce the following output −
+-------------+ | StudentName | +-------------+ | Chris | | David | | Sam | | Mike | +-------------+ 4 rows in set (0.00 sec)
- Related Articles
- Select into a user-defined variable with MySQL
- MySQL ORDER BY with numeric user-defined variable?
- Perform MySQL SELECT INTO user-defined variable
- Set custom messages by working with MySQL IF Statements and SELECT in a user-defined variable
- MongoDB query to set user defined variable into query?
- Updating a MySQL table row column by appending a value from user defined variable?
- In MySQL, why a client cannot use a user-defined variable defined by another client?
- How to set different auto-increment ids for two tables with a user-defined variable?
- Using variables with MySQL prepare statement
- Show a MySQL user-defined variables values in the result table?
- Set user variable from result of query in MySQL?
- How Can we permanently define user-defined variable for a client in MySQL?
- MySQL CREATE USER with a variable?
- Using User-Defined Variables in MySQL
- How can we use SET statement to assign a SELECT result to a MySQL user variable?

Advertisements