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

ssj

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.

Bill Karwin

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can someone help me work out the kinks in my program?

From Dev

Can someone help me on converting this Kotlin function with callback to a java function?

From Dev

I can't solve this error. Can someone help me?

From Dev

can someone help me with this mysql code

From Dev

can someone help figure out this error?? (C++)

From Dev

Can someone help me clean up my r function?

From Dev

Can someone help me for convert this c# recursive function to java

From Dev

Can someone help me with SQL Error: ORA-00904?

From Dev

can someone help me understand why VS Code is throwing this error?

From Dev

can someone help me understand why VS Code is throwing this error?

From Dev

New to ruby, can someone help me decipher this error from console?

From Dev

Python: Can someone help me debug this error? [newbie]

From Dev

Can someone help me with this pattern

From Dev

Can someone help me with this method?

From Dev

Can someone help me portforward?

From Dev

I'm using Linux, compiling with gcc, getting error: warning: implicit declaration of function ‘fopen_s’, can someone help me fix this?

From Dev

paypal parameater to create recurring profile is giving error can one please help me out

From Dev

Can someone point out a small logic error to me?

From Dev

Can someone help point out to me what is wrong with this no brainer WHERE clause?

From Dev

Can someone help me to figure out how to read my file from ":" (not included) til the end of the line?

From Dev

Can someone help me make this code shorter?

From Dev

Can someone help me fix this jumpscript?

From Dev

Can someone help me write this if/else in Jquery

From Dev

Can someone please help me identify these chips?

From Dev

Can someone help me understand this for loop in python

From Dev

Can someone help me with installing Devise?

From Dev

Can someone help me with parallel processing for Python?

From Dev

Can someone help me find freezing cause?

From Dev

Can someone help me with timer and sound events?

Related Related

  1. 1

    Can someone help me work out the kinks in my program?

  2. 2

    Can someone help me on converting this Kotlin function with callback to a java function?

  3. 3

    I can't solve this error. Can someone help me?

  4. 4

    can someone help me with this mysql code

  5. 5

    can someone help figure out this error?? (C++)

  6. 6

    Can someone help me clean up my r function?

  7. 7

    Can someone help me for convert this c# recursive function to java

  8. 8

    Can someone help me with SQL Error: ORA-00904?

  9. 9

    can someone help me understand why VS Code is throwing this error?

  10. 10

    can someone help me understand why VS Code is throwing this error?

  11. 11

    New to ruby, can someone help me decipher this error from console?

  12. 12

    Python: Can someone help me debug this error? [newbie]

  13. 13

    Can someone help me with this pattern

  14. 14

    Can someone help me with this method?

  15. 15

    Can someone help me portforward?

  16. 16

    I'm using Linux, compiling with gcc, getting error: warning: implicit declaration of function ‘fopen_s’, can someone help me fix this?

  17. 17

    paypal parameater to create recurring profile is giving error can one please help me out

  18. 18

    Can someone point out a small logic error to me?

  19. 19

    Can someone help point out to me what is wrong with this no brainer WHERE clause?

  20. 20

    Can someone help me to figure out how to read my file from ":" (not included) til the end of the line?

  21. 21

    Can someone help me make this code shorter?

  22. 22

    Can someone help me fix this jumpscript?

  23. 23

    Can someone help me write this if/else in Jquery

  24. 24

    Can someone please help me identify these chips?

  25. 25

    Can someone help me understand this for loop in python

  26. 26

    Can someone help me with installing Devise?

  27. 27

    Can someone help me with parallel processing for Python?

  28. 28

    Can someone help me find freezing cause?

  29. 29

    Can someone help me with timer and sound events?

HotTag

Archive