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;
Related
How can I write this:
IF DB_ID('DB_NAME') IS NOT NULL DROP DATABASE MORPG1;
GO
CREATE DATABASE DB_NAME;
GO
USE DB_NAME
GO
All operations except USE can be performed with
CREATE PROCEDURE recreate_datebase (dbname VARCHAR(64))
BEGIN
IF EXISTS ( SELECT NULL
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = dbname ) THEN
SET #sql := CONCAT('DROP DATABASE ', dbname);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DROP PREPARE stmt;
END IF;
SET #sql := CONCAT('CREATE DATABASE ', dbname);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DROP PREPARE stmt;
END
Making the database current one cannot be parametrized / executed dynamically.
I am finding it hard to get a MySQL stored procedure to work when I use the SUBSTRING_INDEX and MID and LOCATE functions. The first Select I have works, the other 2 do not. Any help in getting this to work would be awesome.
CREATE DEFINER=`root`#`localhost` PROCEDURE `Address`(TBL VARCHAR(50))
BEGIN
SET #table_name = TBL;
SELECT CONCAT("UPDATE kalo.",#table_name,' SET AD1 = REPLACE(AD1,"."," ")') INTO #SQL;
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT CONCAT("UPDATE kalo.",#table_name,' SET STNO = SUBSTRING_INDEX(AD1, ' ', +1)') INTO #SQL;
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT CONCAT("UPDATE kalo.",#table_name,' SET STNM = MID(AD1, LOCATE(' ', AD1) + 1)') INTO #SQL;
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
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;
Can somebody look at my code ,because I dont know if its OK or no in MySQL
set #max=concat('select max(length(CommentsId))from', table_name);
prepare stmt from #max;
execute stmt;
set #max=concat('UPDATE', table_name, 'SET CommentsId= ',CommentsId * power(10, (#max - length(CommentsId))),' WHERE CommentsId= ', #CommentsId );
prepare stmt from #max;
execute stmt;
You need spaces before or after your from, UPDATE, and SET keywords.
set #max=concat('select max(length(CommentsId)) from ', table_name);
prepare stmt from #max;
execute stmt;
set #max=concat('UPDATE ', table_name, ' SET CommentsId= ',CommentsId * power(10, (#max - length(CommentsId))),' WHERE CommentsId= ', #CommentsId );
prepare stmt from #max;
execute stmt;