简体   繁体   中英

MySQL Error 1064 - 'Define Function' Syntax

I am barely learning MySQL (moving from SQLite). I am trying to create a custom function within the MySQL command line client, but am hitting errors that I can't correct - and can't find answers to online. My first function began as this:

    CREATE FUNCTION myDatabase.LCASE_ASCII_VALUE_AT(
        unparsedLine VARCHAR(1024),
        linePos int
    ) RETURNS int DETERMINISTIC
    return ASCII( LCASE( RIGHT( CHAR_LENGTH(unparsedLine) - linePos + 1) ) );

I got this error:

ERROR 1064 (42000): 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

Any pointers on where my syntax went wrong? I suspected these, but they didn't fix the problem:

  1. Perhaps MySQL does not like compound statements like return ASCII( LCASE( ... . But even after manually unraveling the statement, I still got the same error (but on a line in the newly unraveled statement - I leave out the unraveled statement to save space).
  2. Perhaps the DELIMITER $$ [function definition] END $$ DELIMITER ; business is mandatory - but that didn't fix the error, and MySQL documentation gives an example of a one-line function where DELIMITER is not needed. (the 'hello' function at https://dev.mysql.com/doc/refman/5.0/en/create-procedure.html )

EDIT: Added that I am creating this function within MySQL command line client.

right requires two arguments, the string itself and the number of characters. You've only given the character count.

Try this instead:

return ASCII( LCASE( RIGHT( unparsedLine, CHAR_LENGTH(unparsedLine) - linePos + 1) ) );

Also, you may find that substr is a better option since there's no need to calculate the argument to right in that case, something like:

return ASCII( LCASE( SUBSTR( unparsedLine, linePos, 1 ) ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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