I'm trying to create a table with a name based on the current year and month(2011-09), but MySQL doesn't seem to like this.
SET #yyyy_mm=Year(NOW())+'-'+Month(NOW());
CREATE TABLE `survey`.`#yyyy_mm` LIKE `survey`.`interim`;
SHOW TABLES IN `survey`;
+-----------+
| interim |
+-----------+
| #yyyy_mm |
+-----------+
If I do CREATE TABLE; without the ticks around #yyyy_mm, I get a generic syntax error.
#yyyy_mm resolves to 2020.
You should be able to do something like this:
SET #yyyy_mm=DATE_FORMAT(now(),'%Y-%m');
SET #c = CONCAT('CREATE TABLE `survey`.`',#yyyy_mm, '` LIKE `survey`.`interim`');
PREPARE stmt from #c;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
set #yyyy_mm=concat(year(now()),'-',month(now()));
set #str = concat('create table survery.`', #yyyy_mm,'` like survey.interim;');
prepare stmt from #str;
execute stmt;
deallocate prepare stmt;
Related
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;
How do I create a table in my schema using the current date or timestamp as the naming convention?
CREATE Table CURRENT_DATE|TIMESTAMP;
REPLACE CURRENT_DATE|TIMESTAMP
SELECT *
FROM other.othertable;
I managed to solve it, for those interested.
SET #YYYY_MM_DD_HH_MM_SS=date_format((SELECT NOW() FROM DUAL),'%Y-%m-%d %h:%m:%s');
SET #c = CONCAT('CREATE TABLE `new`.`',#YYYY_MM_DD_HH_MM_SS, '` LIKE `old`.`oldtable`');
PREPARE stmt from #c;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET #u = concat('REPLACE `new`.`',#YYYY_MM_DD_HH_MM_SS, '` SELECT * FROM `old`.`oldtable`');
PREPARE stmt2 from #u;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
Here is the simplified query that doesn't work.
SET #abc = CONCAT('%','string','%');
SET #query = CONCAT('SELECT *
FROM table
WHERE column LIKE ',#abc);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I need to use CONCAT with SELECT because there lots of other variables in real query.
Real query works fine when I use some simple COLUMN=xyz in WHERE clause. But nothing works when I try to use LIKE %xyz%...
Use it like this
SET #abc = CONCAT('"%','string','%"');
SET #query = CONCAT('SELECT *
FROM table
WHERE column LIKE ',#abc);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Check the first line I have added " to show #abc like "%string%"
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;
I would like to make dynamic queries in within my procedure.
SQL Server has neat sp_executesql procedure for such tasks, is there anything in MySQL which can help me to achieve similar functionality?
AFAIK there is nothing exactly same. However, you can use a prepared statement, like:
mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql> SET #a = 3;
mysql> SET #b = 4;
mysql> EXECUTE stmt1 USING #a, #b;
+------------+
| hypotenuse |
+------------+
| 5 |
+------------+
mysql> DEALLOCATE PREPARE stmt1;
Copied from here:
http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
You will probably have to use prepared statements for that purpose using MySQL:
SET #query = 'INSERT INTO tbl (a) VALUES (?)';
PREPARE stmt FROM #query;
...
SET #param = 'hello';
EXECUTE stmt USING #param;
...
DEALLOCATE PREPARE stmt;