2

Hy, I have the following function on my mysql schema, this function should work like this:

  if (especie=mEspecie && variedad=mVariedad) then
         then return precio;   else
         return 0; 
  end if;

but somehow it's not working, anybody can help me here?.

this is my function, I've updated since @vipin answer:

  CREATE DEFINER=`root`@`localhost` FUNCTION `BUSCA_PRECIO_MATRIZ`(mEspecie int, mVariedad int) RETURNS int(11)
    READS SQL DATA
    DETERMINISTIC
BEGIN
declare precio, especie,variedad integer;
declare cur1 cursor for
select ifnull(valor,0),idconf_especie,idconf_variedad from cc_matriz_precios_facturacion_recibidor where idconf_especie=mEspecie and idconf_variedad=mVariedad;

open cur1;
    loop_cur : loop 
        fetch cur1 into precio,especie,variedad;
            if(especie = mEspecie) then
                if  (variedad = mVariedad) then
                    return variedad;
                    LEAVE loop_cur;
                else
                    return variedad;
                    leave loop_cur;
                end if;
            end if;
    end loop;
    return 0;
close cur1;
return 0;
END
6
  • why you written again and again return 0 or 1 ? what you want to write in procedure ? Commented Mar 23, 2016 at 14:40
  • want it to return 0 if not found or precio if found Commented Mar 23, 2016 at 15:01
  • so why you write cursor for it ? you can use it by only select query. i am update my query Commented Mar 23, 2016 at 15:06
  • because I use the result on other query's so I just wanted to call the function Commented Mar 23, 2016 at 15:12
  • maybe it is because Im comparing a string from my cursor with a int value from my function arguments??? Commented Mar 23, 2016 at 15:17

2 Answers 2

2

in my query, if any data found in cc_matriz_precios_facturacion_recibidor table after check condition then its return true other wise its return false

    CREATE FUNCTION `BUSCA_PRECIO_MATRIZ`(mEspecie int, mVariedad int) RETURNS int(11)
        READS SQL DATA
        DETERMINISTIC
    BEGIN
    declare precio, especie,variedad, flag int;    

    select valor,idconf_especie,idconf_variedad, 1 into precio, especie,variedad, flag 
    from cc_matriz_precios_facturacion_recibidor
    where idconf_especie=mEspecie and idconf_variedad=mVariedad;
    IF(flag) THEN 
         return 1; 
    ELSE 
         return 0;
    END IF;
    END

or in short

CREATE FUNCTION `BUSCA_PRECIO_MATRIZ`(mEspecie int, mVariedad int) RETURNS int(11)
            READS SQL DATA
            DETERMINISTIC
        BEGIN
        declare precio, especie,variedad int;   
        DECLARE flag int default 0;

        select valor,idconf_especie,idconf_variedad, 1 into precio, especie,variedad, flag 
        from cc_matriz_precios_facturacion_recibidor
        where idconf_especie=mEspecie and idconf_variedad=mVariedad;
        return flag;
        END
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response but it keep throwing the same response 'OK' I updated my function
1

Another option which simplifies its function is:

DELIMITER //

DROP FUNCTION IF EXISTS `BUSCA_PRECIO_MATRIZ`//

CREATE FUNCTION `BUSCA_PRECIO_MATRIZ` (
    `mEspecie` INT,
    `mVariedad` INT
)
RETURNS BOOL
READS SQL DATA
DETERMINISTIC
BEGIN
    RETURN EXISTS (SELECT NULL
                   FROM `cc_matriz_precios_facturacion_recibidor`
                   WHERE `idconf_especie` = `mEspecie` AND
                         `idconf_variedad` = `mVariedad`);
END//

DELIMITER ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.