using “if” and “else” Stored Procedures MySQL

前端 未结 3 1605
悲哀的现实
悲哀的现实 2021-01-01 11:52

I\'m having some difficulties when trying to create this stored procedure, any kind of help is welcome:

create procedure checando(in nombrecillo varchar(30),         


        
3条回答
  •  梦谈多话
    2021-01-01 12:21

    I think that this construct: if exists (select... is specific for MS SQL. In MySQL EXISTS predicate tells you whether the subquery finds any rows and it's used like this: SELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);

    You can rewrite the above lines of code like this:

    DELIMITER $$
    
    CREATE PROCEDURE `checando`(in nombrecillo varchar(30), in contrilla varchar(30), out resultado int)
    
    BEGIN
        DECLARE count_prim INT;
        DECLARE count_sec INT;
    
        SELECT COUNT(*) INTO count_prim FROM compas WHERE nombre = nombrecillo AND contrasenia = contrilla;
        SELECT COUNT(*) INTO count_sec FROM FROM compas WHERE nombre = nombrecillo;
    
        if (count_prim > 0) then
            set resultado = 0;
        elseif (count_sec > 0) then
            set resultado = -1;
        else 
            set resultado = -2;
        end if;
        SELECT resultado;
    END
    

提交回复
热议问题