MySQL CREATE FUNCTION 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6740932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:32:40  来源:igfitidea点击:

MySQL CREATE FUNCTION Syntax

mysqlfunction

提问by Mel

I am trying to create a function in MySQL:

我正在尝试在 MySQL 中创建一个函数:

Here is the SQL code:

这是SQL代码:

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?

我在 phpMyAdmin 中运行这个 create 语句。这个功能有什么问题?

回答by james_bond

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 ;

回答by biswarupadhikari

MySQL create function syntax:

MySQL 创建函数语法:

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

观看此视频,了解如何创建 mysql 函数以及如何在查询中使用

Create Mysql Function Video Tutorial

创建Mysql函数视频教程