prepared statement in stored procedure not refreshing - mysql

Using a dynamically created view WITHIN a Loop in this MySQL stored procedure. The problem I'm having is the contents of temp_controller_view view only reflect the result from the view's first creation (e.g. first loop) as if the statement CREATE OR REPLACE VIEW isn't being read.
Obviously I'm trying to get the view to update upon each Looping so that I can work with the contents of the dynamic view
MYSQL ##VERSION > 5.1.61
DROP procedure if exists SITETOEPRISE;
DELIMITER //
CREATE PROCEDURE SITETOEPRISE()
BEGIN
DECLARE bDone INT;
DECLARE sid int(11); -- or approriate type
DECLARE curr_site_db VARCHAR(10);
DECLARE Var3 VARCHAR(50);
DECLARE site_curs CURSOR FOR SELECT siteid FROM sites;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
OPEN site_curs;
SET bDone = 0;
site_loop : LOOP
FETCH site_curs INTO sid;
IF bDone THEN
CLOSE site_curs;
LEAVE site_loop;
END IF;
select sid as 'sid_in_loop';
SET #dyn_sql = CONCAT('CREATE OR REPLACE VIEW `temp_controller_view` AS SELECT * from site',sid,'.controller;');
select #dyn_sql;
PREPARE stmt_dyn_view FROM #dyn_sql;
EXECUTE stmt_dyn_view;
DEALLOCATE PREPARE stmt_dyn_view;
select * from temp_controller_view;
drop view temp_controller_view;
END LOOP;
END
//
DELIMITER ;

Related

Get Columns Single Record in Stored Procedure Mysql

I have my procedure and is working, but my question is the following,
with the cursor is working correctly, but before the cursor I need a Single Record with several columns, I don´t know if I need another cursor just for one record.
Which would be the right way to get the columns of that single row without a cursor.
The query to execute is:
'SELECT id,anio,fec_iniciointeres FROM mytable WHERE id=3 '
DELIMITER $$
DROP PROCEDURE IF EXISTS db.cal_intereses$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `cal_intereses`()
BEGIN
DECLARE factura_id INT UNSIGNED;
DECLARE val_avaluo DECIMAL(16,2);
DECLARE fec_actual DATE;
DECLARE done INT;
DECLARE cur CURSOR FOR SELECT fac_facturas.id AS factura_id, fac_facturas.val_avaluo FROM fac_facturas WHERE fac_facturas.vigencia_id<=26 AND fac_facturas.estado=1 AND fac_facturas.val_avaluo>0 LIMIT 10;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
SET fec_actual=(SELECT CURDATE());
SET done = 0;
OPEN cur;
ciclo: LOOP
FETCH cur INTO factura_id,val_avaluo;
IF done=1 THEN LEAVE ciclo; END IF;
DELETE FROM val_interesaux;
IF fec_actual>='2006-07-29' THEN
INSERT INTO val_interesaux(factura_id,fec_inicio) VALUES(factura_id,fec_actual);
END IF;
END LOOP ciclo;
CLOSE cur;
END$$
DELIMITER ;

Loop over table and call stored procedure

Right now, i am trying to loop over a temporary table, that an import process created. I've tried using a cursor, but the tablename must be passed to the stored procedure.
CREATE PROCEDURE `do_update`(tablename VARCHAR(100))
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE c_ean VARCHAR(20);
DECLARE c_sku VARCHAR(20);
DECLARE c_mpn VARCHAR(20);
DECLARE c_manufacturerName VARCHAR(100);
DECLARE c_manufacturerUniqueId VARCHAR(100);
DECLARE c_images TEXT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
-- move declare cursor into sql to be executed
SET #sqlstatement = CONCAT('DECLARE import_cursor CURSOR FOR SELECT ean, sku, mpn, manufacturerName, manufacturerUniqueId, images FROM ', tablename);
PREPARE stmt FROM #sqlstatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
OPEN import_cursor;
importloop: LOOP
FETCH import_cursor INTO c_ean, c_sku, c_mpn, c_manufacturerName, c_manufacturerUniqueId, c_images;
IF done THEN
LEAVE importloop;
END IF;
CALL upsert_article(c_ean, c_sku, c_mpn, c_manufacturerName, c_manufacturerUniqueId, c_images);
END LOOP testloop;
CLOSE import_cursor;
END
Note: This import script will be called several times over the day and could be run concurrently. So a defined tablename is not an option.
What i have tried so far:
Creating a cursor with given tablename. --> Works, but the name must be dynamic.
Creating a cursor with dynamic sql. --> Failed
Skip using cursors and loop over table in a different way. --> No method found for using dynamic sql
So, is there a way to make this work with a cursor or a workaround?
Creating a temporary table and inserting the results of my dynamic query did the trick at the end.
With that i was able to use mysql cursor with the static named temporary table. And since it is an temporary table, multiple import-jobs won't interfere with each other, because they are hidden to other sessions.
In the end this is the code, that worked for me:
CREATE PROCEDURE `do_update`(IN
tablename VARCHAR(100))
BEGIN
DECLARE done INT;
DECLARE c_ean VARCHAR(20);
DECLARE c_sku VARCHAR(20);
DECLARE c_mpn VARCHAR(20);
DECLARE c_manufacturerName VARCHAR(100);
DECLARE c_manufacturerUniqueId VARCHAR(100);
DECLARE c_images NVARCHAR(3000);
DECLARE import_cursor CURSOR FOR SELECT ean, sku, mpn, manufacturerName, manufacturerUniqueId, images FROM importjob;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
DROP TEMPORARY TABLE IF EXISTS importjob;
SET #sqlstatement = CONCAT('CREATE TEMPORARY TABLE importjob SELECT ean, sku, mpn, manufacturerName, manufacturerUniqueId, images FROM ', tablename);
PREPARE stmt FROM #sqlstatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET #DEBUG = true;
OPEN import_cursor;
importloop: LOOP
FETCH import_cursor INTO c_ean, c_sku, c_mpn, c_manufacturerName, c_manufacturerUniqueId, c_images;
IF done THEN
LEAVE importloop;
END IF;
CALL upsert_article(c_ean, c_sku, c_mpn, c_manufacturerName, c_manufacturerUniqueId, c_images);
END LOOP importloop;
CLOSE import_cursor;
DROP TEMPORARY TABLE IF EXISTS importjob;
END

Error (2014. Commands out of querys) with all complex querys on MySQL

On all complex querys (returning many results and running long) I get the same error after some time: Error Code: 2014. Commands out of sync; you can't run this command now
e.g.:
## Prozedur Droppen
DROP PROCEDURE IF EXISTS ifob.uspUpdateHeatStatsAll;
SET SQL_SAFE_UPDATES = 0;
## Prozedur erstellen
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `uspUpdateHeatStatsAll`()
BEGIN
## Update table
DECLARE _HeatNrDINT int;
DECLARE _count int;
DECLARE _act int;
# Bearbeitungsliste erstellen
DROP TABLE IF EXISTS tmptblColumns;
CREATE TEMPORARY TABLE tmptblColumns (`HeatNrDINT` int) ENGINE = MEMORY;
# Delete old data
# Add columns to workertable
INSERT INTO tmptblColumns (`HeatNrDINT`) SELECT DISTINCT `Input.General.Values.HeatNrDINT` from tblmeasuringvaluesyear;
SET _count = (SELECT count(*) FROM tmptblColumns);
SET _act = 1;
#debug
#SELECT * FROM tmptblColumns;
BEGIN
DECLARE _done BOOL DEFAULT FALSE;
DECLARE column_cursor CURSOR FOR SELECT `HeatNrDINT` FROM `tmptblColumns`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = TRUE;
#Alte Daten löschen
OPEN column_cursor;
FETCH column_cursor INTO _HeatNrDINT;
read_loop: LOOP
SELECT CONCAT('Bearbeite:',_HeatNrDINT,'(',_act,'/', _count,')') as `Status`;
CALL uspUpdateHeatStats(_HeatNrDINT);
SET _act = _act+1;
FETCH column_cursor INTO _HeatNrDINT;
IF _done THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE column_cursor;
# Execute STMT
END;
END$$
DELIMITER ;
#TEST
CALL uspUpdateHeatStatsAll();
SELECT * FROM tblheatdata;
works for 50 loops and then it quits.
I have also run into this problem. The maximum number of result sets in MySQL Workbench defaults to 50. It will crash after 50 because the previous results have not been retrieved.

Cursor in Procedure does not return any values

I have a database where temp tables are created, those table names are randomly generated and saved in Checkouts.unid. I want to drop all those tables and truncate the table Checkouts. I thought the niftiest solution would be a procedure, but it will not work:
DROP PROCEDURE IF EXISTS `spCheckoutsCleanup`;
CREATE PROCEDURE `spCheckoutsCleanup` ()
SQL SECURITY INVOKER
BEGIN
DECLARE `t` VARCHAR(64);
DECLARE `ch` CURSOR FOR SELECT `unid` FROM `Checkouts`;
OPEN `ch`;
drop_tables: LOOP
FETCH `ch` INTO `t`;
DROP TABLE IF EXISTS `t`;
END LOOP;
CLOSE `ch`;
TRUNCATE TABLE `Checkouts`;
END
I always get "No data - zero rows fetched, selected, or processed" although those tables are there and the table Checkouts is not empty though.
You have to add something like this in order to end your loop:
DECLARE CONTINUE HANDLER FOR NOT FOUND SET ...;
See example in the documentation.. E.g. ...
DROP PROCEDURE IF EXISTS `spCheckoutsCleanup`;
CREATE PROCEDURE `spCheckoutsCleanup` ()
SQL SECURITY INVOKER
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE `t` VARCHAR(64);
DECLARE `ch` CURSOR FOR SELECT `unid` FROM `Checkouts`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN `ch`;
drop_tables: LOOP
FETCH `ch` INTO `t`;
IF done THEN
LEAVE drop_tables;
END IF;
DROP TABLE IF EXISTS `t`;
END LOOP;
CLOSE `ch`;
TRUNCATE TABLE `Checkouts`;
END
Otherwise you will get an error once you reached the end of your cursor.
I got it working with this:
DROP PROCEDURE IF EXISTS `spCheckoutsCleanup`;
CREATE PROCEDURE `spCheckoutsCleanup` ()
SQL SECURITY INVOKER
BEGIN
DECLARE done int DEFAULT 0;
DECLARE t CHAR(64);
DECLARE ch CURSOR FOR SELECT `unid` FROM `Checkouts`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN ch;
drop_table: LOOP
FETCH ch INTO t;
IF done = 1 THEN
LEAVE drop_table;
END IF;
SET #sql := CONCAT('DROP TABLE IF EXISTS `', t, '`');
PREPARE dropt FROM #sql;
EXECUTE dropt;
END LOOP;
CLOSE ch;
TRUNCATE TABLE `Checkouts`;
END;
CALL spCheckoutsCleanup;

Deleting temporary tables with a stored procedure

I have been trying to make a stored procedure which autodeletes temporary tables.
CREATE PROCEDURE DeleteTemp()
BEGIN
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE tmptablename VARCHAR(100);
DECLARE tmpTables CURSOR FOR
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA='myDB' AND TABLE_NAME LIKE 'tmp%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
OPEN tmpTables;
SELECT FOUND_ROWS() into num_rows;
the_loop: LOOP
FETCH tmpTables INTO tmptablename;
DROP TABLE tmptablename;
IF no_more_rows THEN
CLOSE tmpTables;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END
However, all I get is:
Query : call DeleteTemp
Error Code : 1051
Unknown table 'tmptablename'
How can I pass the variable tmptablename properly into the "DROP TABLE" command?
Fixed using prepared statements.
CREATE
PROCEDURE DeleteTemp()
BEGIN
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE tmptablename VARCHAR(100);
DECLARE tmpTables CURSOR FOR
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA='MY_SCHEMA'
AND TABLE_NAME LIKE 'tmp%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
OPEN tmpTables;
SELECT FOUND_ROWS() INTO num_rows;
the_loop: LOOP
FETCH tmpTables INTO tmptablename;
IF no_more_rows THEN
CLOSE tmpTables;
LEAVE the_loop;
ELSE
SET #a:=CONCAT('DROP TABLE ',tmptablename);
PREPARE stmt1 FROM #a;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END IF;
END LOOP the_loop;
END
Have you had a look at the PREPARE and EXECUTE commands. This is like executing DYNAMIC SQL
SQL Syntax for Prepared Statements
The following SQL statements can be
used in prepared statements: ALTER
TABLE, CALL, COMMIT, CREATE INDEX,
CREATE TABLE, DELETE, DO, DROP INDEX,
DROP TABLE, INSERT, RENAME TABLE,
REPLACE, SELECT, SET, UPDATE, and most
SHOW statements. ANALYZE TABLE,
OPTIMIZE TABLE, and REPAIR TABLE are
also supported as of MySQL 5.0.23.
This may not be what you are after, but are you aware that your temporary tables are only visible to that connection that created them, and will be dropped automatically once that connection is closed?