create function throws error in mysql,can someone

2020-04-24 11:34发布

问题:

I am just trying to create a function which means to check if a table,function or view exists in a mysql database. 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  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.

回答1:

You start four IF statements, but you only have one END IF at the end.

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. 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:

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. There is no else if in mysql?

Not in the way you were using. There is no "ladder" possible with an indefinite number of else-if clauses in standard SQL.

But many languages allow the else block to contain another if/then/else statement. So you can make complex branching code. But you have to terminate each if/then/else statement properly.

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)
  • Ruby (elsif)
  • PHP (elseif)
  • Python (elif)
  • BASIC (elseif)
  • PL/SQL (elsif)
  • PL/pgSQL (elsif)
  • F# (elif)

Languages that do not permit ladders, but do permit nested control structures:

  • C
  • C++
  • C#
  • Objective-C
  • Java
  • Javascript
  • ANSI SQL, Transact-SQL
  • Pascal, Delphi
  • Awk
  • Scala
  • Haskell
  • R
  • Swift
  • Dart
  • Go