Drop table in a MySQL trigger

1

I would like to know if there is a possibility of having the sentence "DROP TABLE TABLATEMPORAL" using MySQL inside a trigger, since when trying to save the trigger with this sentence I get the following error:

"There was an error while applying the SQL script to the database"

CREATE DEFINER='root'@'localhost' TRIGGER 'guardarResultadosFyS' 
AFTER INSERT ON 'mdl_feedback_completed' FOR EACH ROW 
BEGIN

set @existe = (exists (select * from  mdl_feedback_completed fc
    join mdl_feedback_item fi where 
    label in ('Activo/Reflexivo' , 'Secuencial/Global' , 'Sensitivo/Intuitivo' , 'Visual/Verbal')
    and fc.feedback = fi.feedback and   fc.feedback = 3 limit 1));

if @existe !=0 then

    CREATE TEMPORARY TABLE tmp_resultado AS
    SELECT userid, username, fb.course as idcourse, c.feedback, concat(u.firstname, ' ', u.lastname ) as Nombre,
    count(value) as valor, if(value = 1, 
                            SUBSTRING_INDEX(SUBSTRING_INDEX(label, '/',  1), '/', -1), 
                           SUBSTRING_INDEX(SUBSTRING_INDEX(label, '/',  2), '/', -1)) as dimension
    FROM mdl_feedback_item d 
     JOIN mdl_feedback_value f 
     JOIN mdl_feedback_completed c
     JOIN mdl_feedback fb
     JOIN mdl_user u
    WHERE c.userid = new.userid and c.feedback = new.feedback and c.id = f.completed and u.id = c.userid and c.feedback = fb.id and
    label in ('Activo/Reflexivo' , 'Secuencial/Global' , 'Sensitivo/Intuitivo' , 'Visual/Verbal')
    and d.id = f.item group by  label,  VALUE, nombre;

    create temporary table tmp_resultadoPivote as
        select 
        userid,
        username,
        idcourse,
        Nombre ,
        SUM(CASE WHEN dimension = 'Visual' THEN Valor ELSE 0 END) AS 'Visual' ,
        SUM(CASE WHEN dimension = 'Verbal' THEN Valor ELSE 0 END) AS 'Verbal',
        SUM(CASE WHEN dimension = 'Secuencial' THEN Valor ELSE 0 END) AS 'Secuencial' ,
        SUM(CASE WHEN dimension = 'Global' THEN Valor ELSE 0 END) AS 'Global' ,
        SUM(CASE WHEN dimension = 'Sensitivo' THEN Valor ELSE 0 END) AS 'Sensitivo' ,
        SUM(CASE WHEN dimension = 'Intuitivo' THEN Valor ELSE 0 END) AS 'Intuitivo' ,
        SUM(CASE WHEN dimension = 'Activo' THEN Valor ELSE 0 END ) AS 'Activo' ,
        SUM(CASE WHEN dimension = 'Reflexivo' THEN Valor ELSE 0 END) AS 'Reflexivo' 
        from tmp_resultado;

    create temporary table tmp_resultadoResta as
    select userid, username, idcourse, Nombre, 
    (Visual - Verbal) as 'Visual_Verbal' , #visualverbal
    (Secuencial - Global) as 'Secuencial_Global', #secuencialglobal
    (activo - reflexivo) as 'Activo_Reflexivo' , #activoreflexivo
    (sensitivo - Intuitivo) as 'Sensitivo_Intuitivo'  #sensitivointuitivo
    from tmp_resultadoPivote;


    insert into mdl_feldersilverman
    select idcourse, userid, username, nombre, now() as fecha, Visual_Verbal, Secuencial_Global,
    Activo_Reflexivo, Sensitivo_Intuitivo from tmp_resultadoResta order by idcourse;

    drop table tmp_resultado;
    drop table tmp_resultadoPivote;
    drop table tmp_resultadoResta;

    end if;



END

If I delete the section where the drop is, it lets me save the script without problem but once it runs it works fine, but the second one gives me the error that the table "tmp_resultado" exists and that's why I'm looking to eliminate them

    
asked by isivis 05.09.2018 в 07:07
source

1 answer

0

In some stackoverflow comment I have read that you can not drop a temporary table in a trigger but I can not find it in the documentation. In principle, temporary tables are automatically deleted when the connection is closed, but that may not happen in a long time if you use a connection pool in a web application, for example

You can try

  • Delete temporary tables before re-creating them instead of at the end: DROP TEMPORARY TABLE IF EXISTS tmp_resultado;

  • Use permanent tables and empty and refill them each time. You would have to change the CREATE TEMPORARY for INSERT INTO and the result would be the same

  • Use cursors. You create cursors to save the data that you are going to treat in each query and you go through them to complete the data that you need for the final table

answered by 05.09.2018 в 08:26