Creating temporary tables in MySQL Stored Procedure

前端 未结 2 415
感动是毒
感动是毒 2020-12-28 14:34

The following procedure gives me an error when I invoke it using the CALL statement:


CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`(id VARCHA         


        
2条回答
  •  梦谈多话
    2020-12-28 15:10

    I've tidied it up a little for you and added example code. I always keep my parameter names the same as the fields they represent but prefix with p_ which prevents issues. I do the same with variables declared in the sproc body but prefix with v_.

    You can find another one of my examples here:

    Generating Depth based tree from Hierarchical Data in MySQL (no CTEs)

    drop procedure if exists emp_performance;
    
    delimiter #
    
    create procedure emp_performance
    (
    in p_employee_id varchar(10)
    )
    begin
    
    declare v_counter int unsigned default 0;
    
    create temporary table tmp engine=memory select time_in, time_out 
     from attendance where employee_id = p_employee_id;
    
    -- do stuff with tmp...
    
    select count(*) into v_counter from tmp;
    
    -- output and cleanup
    
    select * from tmp order by time_in;
    
    drop temporary table if exists tmp;
    
    end#
    
    delimiter ;
    
    call emp_performance('E123456789');
    

提交回复
热议问题