Table in FROM clause defined by variable in MySQL - mysql

I want to create a SQL script for MySQL 5.7 that inserts data from a table of a database origin into a table of another target database.
I want to have this source-database defined by a variable.
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = concat(#origin_db,'.','tablename');
INSERT INTO target_table SELECT * FROM #origin_table;
Variables are used in various example to define column names but I never seen a way to define a table with it.
Is anyone has a trick for this ?

Variables won't use in table name in MySQL. You only can use a prepared statement for dynamic build query. For example:
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = CONCAT(#origin_db,'.','tablename');
SET #query = CONCAT('INSERT INTO target_table SELECT * FROM ', #origin_table);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
You can read more detail about it in official documentation

You can use Prepared Statement like this:
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = concat(#origin_db,'.','tablename');
SET #qry1 = concat('INSERT INTO target_table SELECT * FROM ', #origin_table);
PREPARE stmt1 from #qry1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

Related

Creating and then using a database with a declared variable name

I'm trying to create a database named 'uppg' and then use that database once it's created.
The first statement executes as it should, but nothing happens when I try to use the database with the variable. I have tried multiple variations but I don't know why it's not working.
SET #dbname = 'uppg';
SET #q1 = CONCAT(
'CREATE DATABASE `', #dbname, '`');
PREPARE stmt1 FROM #q1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET #q2 = CONCAT(
'USE ', #dbname,
);
PREPARE stmt2 FROM #q2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
There exists complete list of statements allowed in PREPARE: SQL Syntax Permitted in Prepared Statements.
USE is not mentioned in it. So you cannot alter current database using dynamic SQL.
The simplest solution: use complete tables names (including database name) in your queries. In this case the current DB can be any (but not none) - this won't effect your queries.

MySQL query, create table with variable

How do you include a variable into a create table query(.sql file)? I have tried everything to my knowledge, but it simply sets the #variable name itself as the table name instead of the actual variable.
(I.e it sets #preset as the name instead of "cart_")
SET #Preset='cart_';
CREATE TABLE IF NOT EXISTS `#preset,Customer` (....
You need dynamic sql. To do this, you are going to have to use prepared statements
Try something like:
SET #SQL = CONCAT('CREATE TABLE ',CONCAT('cart_',customer), ..;
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Paramater for name of a table without using prepare statement in MySQL

can i somehow set parameter for name of table in query without using a prepare statement?
This is example:
SET #tableName = 'Customer';
SELECT * FROM #tableName;
Thanks
Depending on the version of MySQL you are using, you may be able to use something like:
SET #tableName = 'Customer';
SET #s = CONCAT('SELECT * FROM ', #tableName);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

How to get variable value as table name in select statement

SET Project_List_val=CONCAT(Project_Number_val,'_List');
Insert Into test (Manthan_Panel_Id) select Manthan_Panel_Id from Project_List_val where Project_Number_val='9';
In the insert statement there is the variable named 'Project_List_val' which consist of table name as concated in the above step. This statement is not taking the content of the variable as table name instead it is taking 'Project_List_val' as table name and giving table not found error.
Any suggestions?
By default you cannot parameterized table names and column names so you need to create Dynamic SQL for that,
SET #Project_List_val = CONCAT(Project_Number_val, '_List');
SET #projNum = 9;
SET #sql = CONCAT(' INSERT INTO test (Manthan_Panel_Id)
SELECT Manthan_Panel_Id
FROM ', #Project_List_val, '
WHERE Project_Number_val = ?');
PREPARE stmt FROM #sql;
EXECUTE stmt USING #projNum;
DEALLOCATE PREPARE stmt;

MySQL User-Defined Variable Values Can't be Used as Identifiers. How do I do this?

According to the manual:
User variables... cannot be used
directly in an SQL statement as an
identifier or as part of an
identifier, such as in contexts where
a table or database name is expected
Which explains why what I've been trying doesn't work:
set #databaseName := 'job_hunt_2';
drop database #databaseName;
create database #databaseName;
use #databaseName;
Is there a way to accomplish this, or is it simply impossible? Thanks!
may be you should try following approach:
set #databaseName := 'job_hunt_2';
SET #s = CONCAT('drop database ', #databaseName);
PREPARE stmt1 FROM #s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;