MySQL CREATE FUNCTION Syntax

MysqlFunction

Mysql Problem Overview


I am trying to create a function in MySQL:

Here is the SQL code:

CREATE FUNCTION F_Dist3D (x1 decimal, y1 decimal) 
RETURNS decimal
DETERMINISTIC
BEGIN 
 DECLARE dist decimal;
 SET dist = SQRT(x1 - y1);
 RETURN dist;
END;

I am 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 10

I am running this create statement in phpMyAdmin. What is wrong with this function?

Mysql Solutions


Solution 1 - Mysql

You have to override your ; delimiter with something like $$ to avoid this kind of error.

After your function definition, you can set the delimiter back to ;.

This should work:

DELIMITER $$
CREATE FUNCTION F_Dist3D (x1 decimal, y1 decimal) 
RETURNS decimal
DETERMINISTIC
BEGIN 
  DECLARE dist decimal;
  SET dist = SQRT(x1 - y1);
  RETURN dist;
END$$
DELIMITER ;

Solution 2 - Mysql

MySQL create function syntax:

DELIMITER //

CREATE FUNCTION GETFULLNAME(fname CHAR(250),lname CHAR(250))
	RETURNS CHAR(250)
	BEGIN
		DECLARE fullname CHAR(250);
		SET fullname=CONCAT(fname,' ',lname);
		RETURN fullname;
	END //
	
DELIMITER ;

Use This Function In Your Query

SELECT a.*,GETFULLNAME(a.fname,a.lname) FROM namedbtbl as a


SELECT GETFULLNAME("Biswarup","Adhikari") as myname;

Watch this Video how to create mysql function and how to use in your query

Create Mysql Function Video Tutorial

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMelView Question on Stackoverflow
Solution 1 - Mysqljames_bondView Answer on Stackoverflow
Solution 2 - MysqlbiswarupadhikariView Answer on Stackoverflow