简体   繁体   English

创建函数在mysql中引发错误,有人可以帮我吗?

[英]create function throws error in mysql,can someone help me out?

I am just trying to create a function which means to check if a table,function or view exists in a mysql database. 我只是试图创建一个函数,该函数意味着检查mysql数据库中是否存在表,函数或视图。 But I get some errors in my database. 但是我的数据库出现一些错误。 Can someone help me out? 有人可以帮我吗?

DELIMITER $$
DROP FUNCTION IF EXISTS check_if_exists$$
CREATE FUNCTION check_if_exists
  (
    object_name VARCHAR(100),
    db_name     VARCHAR(100),
    object_type ENUM('t', 'f', 'v', 'p')
)
RETURNS INT
BEGIN
IF (object_type='t') THEN
SELECT COUNT(1) INTO @f_result
from information_schema.TABLES as t1
where t1.TABLE_SCHEMA=db_name
      and t1.TABLE_NAME=object_name;
ELSE IF (object_type='f') THEN
select count(1) INTO @f_result
FROM information_schema.ROUTINES as info
WHERE info.ROUTINE_SCHEMA = db_name
      AND info.ROUTINE_TYPE = 'FUNCTION' AND info.ROUTINE_NAME = object_name;
ELSE IF (object_type='v') THEN
select count(1) into @f_result
from information_schema.VIEWS as t1
where t1.TABLE_SCHEMA=db_name and t1.TABLE_NAME=object_name;
ELSE IF (object_type='p') THEN
SELECT COUNT(1) INTO @f_result
FROM information_schema.ROUTINES as info
WHERE info.ROUTINE_SCHEMA = db_name
      AND info.ROUTINE_TYPE = 'PROCEDURE'
      AND info.ROUTINE_NAME = object_name;
END IF;
return (@f_result);
END$$
delimiter ;

another thing, the info of mysql: 另一件事,mysql的信息:

mysql  Ver 14.14 Distrib 5.5.37, for Linux (x86_64) using readline 5.1

and the error message is: 错误消息是:

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 31

as you see, the error message is not helpful. 如您所见,错误消息没有帮助。 This definition of function do not depend any user database. 此功能定义不依赖任何用户数据库。 So you can try in your own DBMS. 因此,您可以尝试使用自己的DBMS。

You start four IF statements, but you only have one END IF at the end. 您开始四个IF语句,但最后只有一个END IF

The error message about syntax error near '' indicates that it parsed to the end of the statement, expected to find more syntax (like the balancing END IF for the remaining nested IF statements), and didn't find it. 靠近''有关语法错误的错误消息表示它已解析到语句的末尾,有望找到更多语法(例如,其余嵌套IF语句的平衡END IF ),但未找到。 The syntax error tries to give you context by showing you what text exists in the remaining part of the statement after the error, but if it reaches the end before it discovers the error, then there is no following text to report. 语法错误试图通过向您显示错误后语句其余部分中存在的文本来提供上下文,但是如果在发现错误之前到达末尾,则没有后续文本要报告。

You might consider using the CASE statement instead: 您可以考虑改用CASE语句

DELIMITER $$
DROP FUNCTION IF EXISTS check_if_exists$$
CREATE FUNCTION check_if_exists (
  object_name VARCHAR(100),
  db_name     VARCHAR(100),
  object_type ENUM('t', 'f', 'v', 'p')
)
RETURNS INT
READS SQL DATA
BEGIN
    DECLARE f_result INT DEFAULT 0;
    CASE object_type
    WHEN 't' THEN
        SELECT COUNT(1) INTO f_result
        FROM information_schema.TABLES AS t1
        WHERE t1.TABLE_SCHEMA = db_name 
          AND t1.TABLE_NAME = object_name;
    WHEN 'f' THEN
        SELECT COUNT(1) INTO f_result
        FROM information_schema.ROUTINES AS info
        WHERE info.ROUTINE_SCHEMA = db_name 
          AND info.ROUTINE_TYPE = 'FUNCTION' 
          AND info.ROUTINE_NAME = object_name;
    WHEN 'v' THEN
        SELECT COUNT(1) INTO f_result
        FROM information_schema.VIEWS AS t1
        WHERE t1.TABLE_SCHEMA = db_name 
          AND t1.TABLE_NAME = object_name;
    WHEN 'p' THEN
        SELECT COUNT(1) INTO f_result
        FROM information_schema.ROUTINES as info
        WHERE info.ROUTINE_SCHEMA = db_name 
          AND info.ROUTINE_TYPE = 'PROCEDURE' 
          AND info.ROUTINE_NAME = object_name;
    END CASE;
    RETURN (f_result);
END$$
DELIMITER ;

Re your comment: 发表您的评论:

I am trying to use if...else if...else like any other language. 我正在尝试使用if ... else if ... else像其他任何语言一样。 There is no else if in mysql? 还有没有,如果在MySQL中?

Not in the way you were using. 不像您以前那样使用。 There is no "ladder" possible with an indefinite number of else-if clauses in standard SQL. 在标准SQL中,无限数量的else-if子句是不可能的“阶梯”。

But many languages allow the else block to contain another if/then/else statement. 但是许多语言允许else块包含另一个if / then / else语句。 So you can make complex branching code. 因此,您可以编写复杂的分支代码。 But you have to terminate each if/then/else statement properly. 但是您必须正确终止每个 if / then / else语句。

IF ... THEN /* start 1st statement */
ELSE
    IF ... THEN /* start 2nd statement */
    ELSE
        IF ... THEN /* start 3rd statement */
        ELSE
        END IF /* end 3rd statement */
    END IF /* end 2nd statement */
END IF /* end 1st statement */

Languages that permit ladders: 允许使用梯子的语言:

  • Perl ( elsif ) Perl( elsif
  • Ruby ( elsif ) Ruby( elsif
  • PHP ( elseif ) PHP( elseif
  • Python ( elif ) Python( elif
  • BASIC ( elseif ) 基本( elseif
  • PL/SQL ( elsif ) PL / SQL( elsif
  • PL/pgSQL ( elsif ) PL / pgSQL( elsif
  • F# ( elif ) F#( elif

Languages that do not permit ladders, but do permit nested control structures: 不允许使用梯子但允许嵌套控制结构的语言:

  • C C
  • C++ C ++
  • C# C#
  • Objective-C Objective-C的
  • Java Java的
  • Javascript 使用Javascript
  • ANSI SQL, Transact-SQL ANSI SQL,Transact-SQL
  • Pascal, Delphi 德尔福帕斯卡尔
  • Awk AWK
  • Scala 斯卡拉
  • Haskell 哈斯克尔
  • R [R
  • Swift 迅速
  • Dart
  • Go

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM