Getting Error While Defining Stored Procedure MySQL - mysql

I am trying to define stored procedure in mysql but getting error i am unable to understand what i am doing wrong.
CREATE PROCEDURE distance(lat1 Float,long1 Float,lat2 Float,long2 Float,OUT distance Float)
BEGIN
SET distance = ROUND((ACOS(SIN(RADIANS(lat1))*SIN(RADIANS(lat2))+COS(RADIANS(lat1))*COS(RADIANS(lat2))*COS(RADIANS(lon2-lon1)))*6371), 2);
END
ERROR:
#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 '' at line 4

Use Delimiters to resolve this:
delimiter //
CREATE PROCEDURE distance(lat1 Float,long1 Float,lat2 Float,long2 Float,OUT distance Float)
BEGIN
SET distance = ROUND((ACOS(SIN(RADIANS(lat1))*SIN(RADIANS(lat2))+COS(RADIANS(lat1))*COS(RADIANS(lat2))*COS(RADIANS(lon2-lon1)))*6371), 2);
END //
delimiter ;

Related

Returning Error in a Dynamic MySql Procedure

I am developing some functions/procedures for a much larger system, that is gona be used in others tables, so i tried to create a Dynamic MySql Procedure, to pass the name of the tables by variables,and get an "InOut" variable to return the result that i want, but it is returning me some Syntax error, i don't know where i am geting wrong in the code, so if someone knows a way better to do it, or get where the error is, i will be very thankfull!
Here is The Error that is returning:
Error Code: 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 'If Exists (Select cliente26 From da26s9 Where da26s9.c' at line 1.
Here is how i am Calling the Procedure:
Call FilTabMes('da26s9','codigo26','cliente26','002715',#string);
Here is The Procedure that i am struggling with:
CREATE PROCEDURE `FilTabMes`(vTabMes_ Char(32),vCodMes_ Char(32),vCodTab_ Char(32),vComFin_ Integer,InOut vResPes_ Char(32))
Begin
Declare vSelTab VarChar(1000);
Set #vSelTab = CONCAT('Select ',vCodTab_,' From ',vTabMes_,'
Where ',vTabMes_,'.',vCodMes_," Like ",vComFin_);
Set #vSelTab = CONCAT('If Exists(',#vSelTab,')
Begin
Set vResPes_ = "T";
End
Else
Begin
Set vResPes_ = "F";
End
End If');
Prepare stmt1 From #vSelTab;
Execute stmt1;
End

MySQL Function use provided variables throws error

I want to create a function for calculating the distance between points.
The calculation is going as expected, but i receive an error here:
DROP FUNCTION IF EXISTS CalculateDistance;
CREATE FUNCTION CalculateDistance(breite double, laenge double) RETURNS INT READS SQL DATA
BEGIN
DECLARE breite DOUBLE;
SET #ibk_laenge = breite;
CREATE FUNCTION CalculateDistance(breite double, laenge double) RETURNS INT READS SQL DATA
BEGIN
DECLARE breite DOUBLE
MySQL meldet: Dokumentation
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 '' at line 5
What's wrong with that?
You need to change the delimiter first:
delimiter //
CREATE FUNCTION CalculateDistance ...
END //
delimiter ;
Otherwise the function definition stops at the first ; which would make it incomplete.

Error on Creating Stored Function in MySQL 5.0.5

I'm usually working with mssql or postgres, but I need to use MySql in the current project.
Now I have a problem with creating a stored function in MySQL 5.0.5.
I'm getting the following error:
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 '' at line 4
My Code looks like that:
CREATE FUNCTION aip_sf_choice_valid (request_id INT)
RETURNS INT
BEGIN
DECLARE sf_result INT;
SET sf_result = 1;
RETURN sf_result
END
I'm really out of ideas. I'm glad for any help!
You need to change the default delimiter from ';' within the SP body. More info on that here: http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html
Try this:
DELIMITER $$
CREATE FUNCTION aip_sf_choice_valid (request_id INT)
RETURNS INT
BEGIN
DECLARE sf_result INT;
SET sf_result = 1;
RETURN sf_result;
END
Should it have a semicolon after "END"?

Trouble creating a function in MySQL

I'm used to working in MS SQL Server, so MySQL is always a little bit foreign to me. I wrote the following for MS SQL Server, and I'm trying to port it to MySQL:
CREATE FUNCTION ToRadians
(#Degrees float)
RETURNS float
AS
BEGIN
DECLARE #Radians float
SELECT #Radians = #Degrees * 3.1415927 / 180
RETURN #Radians
END
GO
I've got
CREATE FUNCTION ToRadians
(#Degrees float)
RETURNS float
BEGIN
DECLARE #Radians float;
SELECT #Radians = #Degrees * 3.1415927 / 180;
RETURN #Radians;
END
but in PHPMyAdmin, that gives me the error:
Error
SQL query:
CREATE FUNCTION ToRadians(
#Degrees float
) RETURNS float AS BEGIN DECLARE#Radians float;
MySQL said: Documentation
#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 '#Degrees float)
RETURNS float
AS
BEGIN
DECLARE #Radians float' at line 2
The searching I've done indicates that the above MySQL UDF should be correct, but it obviously isn't. SELECT version() returns 5.0.77
CREATE FUNCTION ToRadians (in_degrees FLOAT) RETURNS FLOAT
DETERMINISTIC
BEGIN
RETURN in_degrees * 3.1415927 / 180;
END;
DETERMINISTIC because the return for a given value of degrees should always be the same in radians.
Also DEGREES() and RADIANS() functions already exist in mysql.
RADIANS()
Don't use # in MySQL variable names, it creates a local session variable.
You probably need to change standart delimiter which is semicolon to another character, for instance // :
DELIMITER // before your function definition, then // just after function, then switch delimiter back to semicolon (DELIMITER ;)

Syntax error of ''?

I can't seem to figure out where this is coming from... MySQL give me a syntax error of an empty quote and gives me a line number that doesn't seem to be wrong at all. Worse still, deleting the loop the line number points to still gives me the same error, just with a different line number.
ERROR 1064 (42000) at line 13: 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 39
Talk about unhelpful feedback from MySQL!
The code in question is a stored function, and I ran into this when trying to apply the answer to another question. The updated code is available here.
EDIT: #MarkByers, here's the function reduced as low as I could get it while still triggering the error:
DROP FUNCTION IF EXISTS months_within_range;
DELIMITER //
CREATE FUNCTION months_within_range(starts_at DATE, ends_at DATE, filter_range VARCHAR(255)) RETURNS TINYINT
BEGIN
SET #matches = 1;
IF #matches >= 1 THEN RETURN 1;
ELSE RETURN 0;
END//
DELIMITER ;
You are missing the END IF
IF #matches >= 1 THEN RETURN 1;
ELSE RETURN 0;
END IF;
And the reason RETURN IF(#matches >= 1, 1, 0); works is because this is the IF function, which is different from the IF statement