MySQL Forums
Forum List  »  Italian

Termporary table in stored function
Posted by: Fabrizio Rizzo
Date: May 18, 2009 05:26PM

Ciao a tutti.
Per la mia applicazione avrei bisogno di una function che mi restituisca un certo valore, frutto di una function che itera su una tabella, creando una tabella temporanea ed eseguendo poi una SUM su di essa per ottenere il risutalto.

Il problema che mi si presenta è che quando poi devo ciclare sulla tabella temporanea questa non esiste. L'errore che mi viene restituito è:

table <nome tabella> doesn't exists. Errore 1146.

Ho letto diversi post che parlano di porre in transazione la query che contiene la creazione della tabella e le operazioni successive, ma in un una stored-function questo non è ammesso.
Cosi come qualcuno consigliava di aggiungere il comando prima della create della tabella temporanea:

SET @@session.pseudo_thread_id=30;

nemmeno questo ha funzionato.

Attualmente ho in uso la versione 5.0.37.

Vi posto la sstored function per intero, utilizzo una subfunction che esegue solo una select


CREATE DEFINER=`mc_admin`@`%` FUNCTION `fCaricaNumeroArticoli`(idCategoria INT, idGruppo INT) RETURNS int(3)
DETERMINISTIC
BEGIN

DECLARE nArticoli INT default 0;
DECLARE currNumArticoli INT default 0;
DECLARE levelCat INT default 1;
DECLARE iCat INT;
DECLARE finito INT DEFAULT 0;

DECLARE cur CURSOR FOR
SELECT cat.id FROM tblCategorie cat
INNER JOIN tmpCategorie father ON (cat.idPadre = father.id)
WHERE father.levelCat = levelCat;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET finito = 1;

DROP TEMPORARY TABLE IF EXISTS tmpCategorie;
CREATE TEMPORARY TABLE tmpCategorie (id INT, levelCat INT, numArt INT);

SELECT fCaricaNumArticoliCategoria(idCategoria, idGruppo) INTO currNumArticoli;
INSERT INTO tmpCategorie VALUES (idCategoria, levelCat, currNumArticoli);

WHILE EXISTS (SELECT cat.id FROM tblCategorie cat
INNER JOIN tmpCategorie father ON (cat.idPadre = father.id)
WHERE father.levelCat = levelCat) DO

OPEN cur;
FETCH cur INTO iCat;
ciclo: WHILE NOT finito DO

SELECT fCaricaNumArticoliCategoria(iCat, idGruppo) INTO currNumArticoli;
INSERT INTO tmpCategorie VALUES (iCat, levelCat+1, currNumArticoli);

FETCH cur INTO iCat;
END WHILE ciclo;

SET levelCat = levelCat + 1;

END WHILE;

SELECT SUM(numArt) INTO nArticoli FROM tmpCategorie;

RETURN nArticoli;
END

L'errore di tabella non trovata è dovuto ad un limite di MySql (ci sono versioni in cui non lo è ?) oppure è la mia function ad essere sbagliata ?

Ringrazio anticipatamente.

Fabrizio.

Options: ReplyQuote


Subject
Views
Written By
Posted
Termporary table in stored function
5646
May 18, 2009 05:26PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.