简体   繁体   中英

MySQL nested case procedure using concat

I'm doing a nested case. However, I am getting an error near this line:

else 
        select concat('The month parameter ', p_month, ' is invalid; cannot proceed.');

This is actually the else case for the most inner case. p_month is an IN parameter and also an integer. Could this be the error?

Any thoughts will be helpful. Thank you.


I played around with it a little bit more right now. So I decided to SELECT on the outside block. However, I know now I have an error for the Select statement in the inner block. How can I fix that? Thanks. entire code:

Create procedure ExamFeesMonth(in p_cl_id int, in p_month int)
begin
    declare count_cl_id int;
    declare num_exam int;
    declare count_exam int;
    declare v_msg varchar(200);


    -- check if p_cl_id is in vt_clients
    select count(*) into count_cl_id from vt_clients where cl_id = p_cl_id; 

    -- count the number of exams that has happened in p_month of previous year 
    select count(*) into num_exam 
    from vt_clients cl 
        join vt_headers h on cl.cl_id = h.cl_id
        join vt_details d on h.ex_id = d.ex_id
    where cl.cl_id = p_cl_id
        and month(ex_date) = p_month
        and year(ex_date) = (year(current_date())-1)
    ;

    select 
    -- first case block starts
    case
    -- client valid 
    when count_cl_id = 1 then

        -- second case block starts
        case 
        -- p_month valid
        when p_month in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) then

            -- third case block starts
            case 
            -- existing exams
            when count_exam >= 1 then
                select concat( 'Client ', p_cl_id, ' has ', count(h.ex_id),  
                                ' exam(s) with total fees of ', sum(ex_fee), 
                                ' in ', year(current_date())-1, '-', p_month)
                from vt_clients cl 
                    join vt_exam_headers h on cl.an_id = h.an_id
                    join vt_exam_details d on h.ex_id = d.ex_id
                where cl_id = p_cl_id 
                    and year(ex_date) = (year(current_date())-1)
                    and month(ex_date) = p_month
                ;
            -- no exams
            when count_exam = 0 then 
             concat( 'No exams for client ',  p_cl_id, ' in 2011-' , p_month, '.');

            -- third case block ends
            end 

        -- p_month invalid      
        else 
             concat('The month parameter ', p_month, ' is invalid; cannot proceed.');           

        -- second case block ends
        end 

    -- client invalid
    when count_cl_id = 0 then
    concat('We have no client with id ', p_cl_id, '; cannot proceed.') ;

    -- first case block ends
    end case;


end;
#

I think the issue in END statements in the nested CASEs. You should use END CASE . CASE...END CASE

       -- third case block ends
        end case;  --  <-------------here

    -- p_month invalid      
    else 
         concat('The month parameter ', p_month, ' is invalid; cannot proceed.');           

    -- second case block ends
    end case; --  <-------------and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM