AmraAz AmraAz - 5 months ago 23
SQL Question

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 22

I am trying to create a function in MySQL:

Here is the SQL code:

DELIMITER //

CREATE FUNCTION hranisez_wp.calcBMI
(
cPol varchar,
dPregled datetime,
dRodjen datetime,
nVisina double,
nTezina double;
)
returns varchar(4000)
begin
declare nBMI double;
declare nStarost double;
declare cBMIText varchar(100);
declare nPothranjen double;
declare nMrsav double;
declare nNormalan double;
declare nGojazan double;
declare nPrekomjeran double;
declare nMinTezina double;
declare nMaxTezina double;
declare exit handler for sqlexception begin return(null);
end
//

DELIMITER ;


And I am getting this error all the time:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' dPregled datetime, dRodjen datetime, nVisina double, nTez' at line 3


Please help!

Answer

There are three errors in your SQL

  • You have a ; after the last parameter
  • varchar need a length parameter
  • The exit handler has no end; for the handler body

So try this:

DELIMITER //

CREATE FUNCTION hranisez_wp.calcBMI
(
    cPol     varchar(50),
    dPregled datetime,
    dRodjen  datetime,
    nVisina  double,
    nTezina  double
)
returns varchar(4000)
begin
declare nBMI         double;
declare nStarost     double;
declare cBMIText     varchar(100);
declare nPothranjen  double;
declare nMrsav       double;
declare nNormalan    double;
declare nGojazan     double;
declare nPrekomjeran double;
declare nMinTezina   double;
declare nMaxTezina   double;
declare exit handler for sqlexception begin return(null); end;
end
//

DELIMITER ;