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%"
Related
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;
If I have the output of a prepared statement; how do I use it as the source of my query?
CALL `myProcedure`;
PREPARE stmnt FROM #allSQL;
EXECUTE stmnt;
DEALLOCATE PREPARE stmnt;
So, I get an output from the EXECUTE stmnt; (let's call it tmp) and I would like to run a query along the lines of:
SELECT * FROM (EXECUTE stmnt) AS tmp WHERE this = that;
You could prepare a CREATE TEMPORARY TABLE statement that contains the result of the query:
SET #createSQL = CONCAT('CREATE TEMPORARY TABLE tmp AS ', #allSQL);
PREPARE stmt FROM #createSQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT * FROM tmp WHERE this = that;
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;
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;