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;
Related
I want to use variable name in drop table statement in mysql.
set #dropable:='table_name';
DROP TABLE IF EXISTS #dropable;
You could try using a prepared statement here:
SET #droptable := 'table_name';
SET #sql := CONCAT('DROP TABLE IF EXISTS ', #droptable);
PREPARE stmt FROM #sql;
EXECUTE stmt;
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;
Is it possible to pass a name of a procedure dynamically within another procedure?
The procedure name is stored in a table and based on criteria the name will be different.
If it is possible how would I accomplish this?
So far I have something like this:
SET $proc = CONCAT('CALL ',$queryString);
PREPARE stmt FROM CONCAT('CALL ', $queryString);
EXECUTE stmt;
Pretty close, just use correct prepared statement syntax:
create procedure sp_exec_proc(
in_proc char(64)
)
begin
set #proc = concat('CALL ',in_proc);
prepare stmt from #proc;
execute stmt;
end
You can then pass in the procedure name and optional parameters
call sp_exec_proc('sp_my_proc("ABC")')
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;
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;