Correct code improvement - mysql

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;

Related

MySQL table creation convention

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;

SUBSTRING_INDEX and MySQL Stored Procedures

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

Error Code: 1193. Unknown system variable 'sys_time_in_current_table'

i am getting following error, while i have tried on google,but didn't find the solution.
Mysql workbench version is 6.2;
Mysql version is 5.6.21.
CREATE DEFINER=`developer`#`%` PROCEDURE `sp_tr069_update_performance_performanceinventory_test`(
in temp_db varchar(256),
in nocout_db varchar(256)
)
BEGIN
declare sys_time_in_current_table int ;
declare sys_time_in_perf_inventory_status int;
set #temporary_query = concat('set sys_time_in_current_table = (select sys_timestamp from ',temp_db,'.','performance_performanceinventory limit 1 )');
PREPARE stmt from #temporary_query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
set #temporary_query1 = concat('set sys_time_in_perf_inventory_status = (select sys_timestamp from ',nocout_db,'.performance_inventorystatus order by id desc limit 1) ;');
PREPARE stmt1 from #temporary_query1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
set #temporary_query=NULL;
-- set #temporary_query=concat();
-- prepare stmt from ;
select `sys_time_in_current_table`,`sys_time_in_perf_inventory_status`;
IF `sys_time_in_current_table`>`sys_time_in_perf_inventory_status`
THEN
select 1;
set #temporary_query2 =
concat('UPDATE ', nocout_db,'.`performance_inventorystatus` inv_status
INNER JOIN ', temp_db,'.`performance_performanceinventory` perf_inv on
inv_status.device_name=perf_inv.device_name and
inv_status.service_name=perf_inv.service_name and
inv_status.data_source=perf_inv.data_source
SET
inv_status.device_name=perf_inv.`device_name` ,
inv_status.service_name=perf_inv.`service_name` ,
inv_status.machine_name=perf_inv.`machine_name` ,
inv_status.site_name=perf_inv.`site_name` ,
inv_status.ip_address=perf_inv.`ip_address` ,
inv_status.data_source=perf_inv.`data_source` ,
inv_status.severity=perf_inv.`severity` ,
inv_status.current_value=perf_inv.`current_value` ,
inv_status.min_value=perf_inv.`min_value` ,
inv_status.max_value=perf_inv.`max_value` ,
inv_status.avg_value=perf_inv.`avg_value` ,
inv_status.warning_threshold=perf_inv.`warning_threshold` ,
inv_status.critical_threshold=perf_inv.`critical_threshold` ,
inv_status.sys_timestamp=perf_inv.`sys_timestamp` ,
inv_status.check_timestamp=perf_inv.`check_timestamp`;') ;
PREPARE stmt2 from #temporary_query2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
set #temporary_query3 = concat('INSERT INTO ',nocout_db,'.`performance_inventorystatus`
(
`device_name`,
`service_name`,
`machine_name`,
`site_name`,
`ip_address`,
`data_source`,
`severity`,
`current_value`,
`min_value`,
`max_value`,
`avg_value`,
`warning_threshold`,
`critical_threshold`,
`sys_timestamp`,
`check_timestamp`)
SELECT
`performance_performanceinventory`.`device_name`,
`performance_performanceinventory`.`service_name`,
`performance_performanceinventory`.`machine_name`,
`performance_performanceinventory`.`site_name`,
`performance_performanceinventory`.`ip_address`,
`performance_performanceinventory`.`data_source`,
`performance_performanceinventory`.`severity`,
`performance_performanceinventory`.`current_value`,
`performance_performanceinventory`.`min_value`,
`performance_performanceinventory`.`max_value`,
`performance_performanceinventory`.`avg_value`,
`performance_performanceinventory`.`warning_threshold`,
`performance_performanceinventory`.`critical_threshold`,
`performance_performanceinventory`.`sys_timestamp`,
`performance_performanceinventory`.`check_timestamp`
FROM ', temp_db,'.`performance_performanceinventory`
left join ', nocout_db,'.performance_inventorystatus inv_status on
performance_performanceinventory.device_name=inv_status.device_name and
performance_performanceinventory.service_name=inv_status.service_name and
performance_performanceinventory.data_source=inv_status.data_source
where inv_status.device_name is null ;');
PREPARE stmt3 from #temporary_query3;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
set #temporary_query4 = concat('INSERT INTO', nocout_db,'.`performance_performanceinventory`
(
`device_name`,
`service_name`,
`machine_name`,
`site_name`,
`ip_address`,
`data_source`,
`severity`,
`current_value`,
`min_value`,
`max_value`,
`avg_value`,
`warning_threshold`,
`critical_threshold`,
`sys_timestamp`,
`check_timestamp`)
SELECT
`performance_performanceinventory`.`device_name`,
`performance_performanceinventory`.`service_name`,
`performance_performanceinventory`.`machine_name`,
`performance_performanceinventory`.`site_name`,
`performance_performanceinventory`.`ip_address`,
`performance_performanceinventory`.`data_source`,
`performance_performanceinventory`.`severity`,
`performance_performanceinventory`.`current_value`,
`performance_performanceinventory`.`min_value`,
`performance_performanceinventory`.`max_value`,
`performance_performanceinventory`.`avg_value`,
`performance_performanceinventory`.`warning_threshold`,
`performance_performanceinventory`.`critical_threshold`,
`performance_performanceinventory`.`sys_timestamp`,
`performance_performanceinventory`.`check_timestamp`
FROM ', temp_db,'.`performance_performanceinventory` ;');
PREPARE stmt4 from #temporary_query4;
EXECUTE stmt4;
DEALLOCATE PREPARE stmt4;
set #temporary_query5 = concat('Truncate ', temp_db,'.`performance_performanceinventory`');
PREPARE stmt5 from #temporary_query5;
EXECUTE stmt5;
DEALLOCATE PREPARE stmt5;
END IF;
END
When i execute this stored procedure,then its done ....
but its throwing run time error when i call this procedure....
call nocout_bhutan_testing.sp_tr069_update_performance_performanceinventory_test('tr069_temp_machine_1', 'nocout_bhutan_testing');

Concat select query with like %string%

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%"

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;