MySQL Forums
Forum List  »  Italian

Re: eseguire una stringa come una query
Posted by: Michele Abbondanza
Date: August 04, 2008 02:08AM

Ciao a tutti!!Sono nuovo e cercando di risolvere il mio problema mi sono imbattuto nel tuo..
Ho lo stesso identico problema, non riesco ad eseguire una query dinamica dentro un trigger.. mi spiego meglio:
ho creato una "stupida" stored che mi esegue del codice arbitrario

CREATE DEFINER=`root`@`localhost` PROCEDURE `eseguiQuery`(queryT VARCHAR(255))
BEGIN
insert into test VALUES (null,queryT,"eseguiQuery");
PREPARE stmt FROM @queryT;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END

innanzitutto questa stored funziona dandole a mano la query,(es: CALL `db`.eseguiQuery("insert into test values(null,'a','b');" )
però non mi funziona dentro un trigger!! il trigger è il seguente:


AFTER INSERT ON valori_attributo
FOR EACH ROW
BEGIN
DECLARE ID_cat INT;
DECLARE finito INT default 0;
DECLARE quer VARCHAR(255);
DECLARE nomeTab VARCHAR(255);
DECLARE nomeAttr VARCHAR(255);
DECLARE lis VARCHAR(255);
DECLARE cur1 CURSOR FOR SELECT ID_categoria FROM `db`.attributi_categorie where ID_attributo = NEW.ID_attributo;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET finito = 1;
OPEN cur1;
FETCH cur1 INTO ID_cat;
ciclo: WHILE NOT finito DO
#------provavalori-------------------------------------------
SET nomeTab = `db`.nomeCat(ID_cat);
SET nomeAttr = `db`.nomeAtt(NEW.ID_attributo);
SET lis = `db`.listaAttributo(NEW.ID_attributo);
set quer = CONCAT("ALTER TABLE `db`.`",nomeTab,"` MODIFY COLUMN `",nomeAttr,"` SET(",lis,") DEFAULT NULL;");
INSERT INTO test VALUES (null,quer,"query trigger");
#INSERT INTO test VALUES (null,NEW.ID_attributo,"ID attr trigger");
#INSERT INTO test VALUES (null,nomeTab,"tabella trigger");
#INSERT INTO test VALUES (null,nomeAttr,"attributo trigger");
#INSERT INTO test VALUES (null,lis,"lista trigger");
#-------------------------------------------------------
#SET quer = CONCAT('"',quer,'"');
CALL `db`.eseguiQuery(quer);
END WHILE ciclo;
END;

(la riga SET quer = CONCAT('"',quer,'"'); è ininfluente, il risultato non cambia)
praticamente mi si blocca tutto appena viene effettuata la prima execute della stored, se commento tutto il blocco di PREPARE/DEALLOCATE invece e mantenendo in sostanza solo le query di insert statiche "gira tutto fino in fondo"..
non riesco a capire dove sto sbagliando neanch'io...
ah, nella tabella test senza il blocco incriminato mi ritrovo la query del trigger,e poi quella della stored PER il numero di record della select.. mentre con l'execute mi fa solo il primo insert del trigger poi nulla più!

scusate se non mi sono fatto capire bene, a disposizione per chiarimenti! (ovviamente sul mio codice, su mysql non è che possa chiarire poi così tanto:) )

Options: ReplyQuote


Subject
Views
Written By
Posted
5749
April 22, 2008 10:07AM
Re: eseguire una stringa come una query
4880
August 04, 2008 02:08AM


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.