Home > Blockchain >  Cant understand my syntax mistake in MySQL CREATE FUNCTION
Cant understand my syntax mistake in MySQL CREATE FUNCTION

Time:07-08

This is my code and I can not find the mistake! I am trying to create a function that multiplies a value to de input parameter and returns that result! It should not be that difficult but I am not finding my sintaxt mistake

CREATE FUNCTION `Costotramite` (monto INT)
    RETURNS INTEGER
    BEGIN
        NO SQL
        DECLARE porcentaje FLOAT;
        SET porcentaje = 0.1;
        DECLARE CostoLicitacion INTEGER;
        SET CostoLicitacion= (monto * porcentaje);
        RETURN CostoLicitacion;
    END

The mistake says: error code 1064

CodePudding user response:

The DECLARE has to be before the code.

I also changed it tio deterministic

CREATE FUNCTION `Costotramite` (monto INT)
RETURNS INTEGER
DETERMINISTIC
BEGIN

  DECLARE porcentaje FLOAT;
  DECLARE CostoLicitacion INTEGER;
  SET porcentaje = 0.1;
  SET CostoLicitacion= (monto * porcentaje);
  RETURN CostoLicitacion;
END
SELECT Costotramite(1000)
| Costotramite(1000) |
| -----------------: |
|                100 |

db<>fiddle here

  • Related