Skip to content
Advertisement

Cant understand my syntax mistake in MySQL CREATE FUNCTION

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement