Trouble creating a function in MySQL - 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 ;)

Related

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.

MySQL Stored Function Syntax Error

I've spent hours and I can't understand why this is highlighted red and I don't know what my error is.
CREATE FUNCTION Student_Section_Count(StudentID INT)
Returns INT
Begin
DECLARE section_Registred INT;
SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count'
FROM Section
Inner Join
Registration on registration.StudentID=Section.ID
Where registration.StudentID=StudentID);
Return Section_Registred;
END$$
Delimiter;
It highlights END and Delimiter, as well as INT from Return INT
By default semi-colon ; is the delimiter in mysql. So when using a mysql client, the function definition that is passed to server is only up to the first semi-colon i.e. DECLARE section_Registred INT;. Rest of the definition of function is not passed to server. This produces an error. To incorporate semi-colon in function definition you need to change your delimiter from semi-colon to some thing else. So before defining the function write the below sql to change the delimiter:
DELIMITER $$.
After the above sql write sql for your function. Append the new delimiter $$ to END at the end of definition of your function. Now the whole definition of function is passed to server up to the new delimiter. This will fix your error. Change the delimiter back to default after your function definition ends as below:
DELIMITER ;.
Also remember to choose a delimiter that is not present anywhere inside your function definition.
Well, I guess I figured it out on my own.
DELIMITER $$
CREATE FUNCTION Student_Section_Count(StudentID INT)
Returns INT
Begin
DECLARE section_Registred INT;
SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count'
FROM Section
Inner Join
Registration on registration.StudentID=Section.ID
Where registration.StudentID=StudentID);
Return Section_Registred;
END$$

Getting Error While Defining Stored Procedure 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 ;

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"?

MySQL 5.1.39 SQL syntax error

I have made an upgrade for my MySQL Server to 5.1.39 and now when I run SQL scripts (which had worked previously) - it throws error. I have checked syntax many times and I couldn't find any incompatible code parts. Please suggest any solution for this problem.
Error message
Mysql::Error: 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 'CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255)) RETURNS V' at line 3:
SQL code:
/*DELIMITER //*/
DROP FUNCTION IF EXISTS clean_dymmy_table;
CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255)) RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE temp_val VARCHAR(255);
SET temp_val = dummy_name;
-- Test
SET temp_val = REPLACE(temp_val, 'Tmp ', '');
SET temp_val = REPLACE(temp_val, ' TmP', '');
SET temp_val = REPLACE(temp_val, 'TMP ', '');
SET temp_val = REPLACE(temp_val, ' TMP', '');
SET temp_val = REPLACE(temp_val, ' tmp', '');
RETURN dummy_name;
END/*//*/
Not sure why you removed the DELIMITER part, but when I add that back in, it runs fine:
DELIMITER // -- you have to change what MySQL expects between commands
DROP FUNCTION IF EXISTS clean_dymmy_table // -- tell it a new command's coming
CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
-- now this can be parsed as part of the current command.
DECLARE temp_val VARCHAR(255);
SET temp_val = dummy_name;
-- Test
RETURN dummy_name;
END
// -- Now you're done with that command.
-- go back to semi-colons, because otherwise life is too zany for me.
DELIMITER ;
(This was in 5.1.54... but I don't think that should matter)
A hunch: There seems to be a MySQL Bug in the 5.1.x line pertaining to DELIMITER that may be biting you here:
#46429 use DELIMITER command in MySql.Data.MySqlClient.MySqlScript
Though that heavily depends on how you're calling it, given cwallenpoole's response, your symptom does suggest it may well be that bug, given your MySQL version. Is upgrading a possibility for you?