我自己定义的函数
DELIMITER $$
DROP FUNCTION IF EXISTS myfunc $$
CREATE FUNCTION myfunc (hash1 varchar(256), hash2 varchar(256), maxlen int)
RETURNS float
DETERMINISTIC
BEGIN
DECLARE hashpart1 varchar(64) DEFAULT "";
DECLARE hashpart2 varchar(64) DEFAULT "";
具体细节略去
RETURN ((64*i)-bitcnt)*100.0/(64*i);
END $$
DELIMITER ;我在my.cnf 中加了
init-file=/var/lib/mysql/startup.sql 
然后在 /var/lib/mysql/startup.sql  把上面的函数粘帖进去,但不能成功
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 'DELIMITER $$' at line 1估计这个是要每行运行的。我把DELIMITER $$ 都去掉,变成DROP FUNCTION IF EXISTS myfunc $$
CREATE FUNCTION myfunc (hash1 varchar(256), hash2 varchar(256), maxlen int)
RETURNS float
DETERMINISTIC
BEGIN
DECLARE hashpart1 varchar(64) DEFAULT "";
DECLARE hashpart2 varchar(64) DEFAULT "";
具体细节略去
RETURN ((64*i)-bitcnt)*100.0/(64*i);
END $$还是不行。
究竟要如何把函数存起来呢?但是如果我进入命令行,允许上面的函数,是成功的。

解决方案 »

  1.   

    The name of the file specified with the --init-file option when you start the server. This should be a file containing SQL statements that you want the server to execute when it starts. Each statement must be on a single line and should not include comments. For more information, see the description of --init-file. 
    -------------------------------- 帮助上写的是每个语句只能在一行上
      

  2.   

    delimiter 不是 sql 语句,所以肯定是不能加的,sql 语句结束是 ;,所以没有 delimiter ,也就不能用 $$
    也就是大致如下是没问题的,注意库名,函数内容改成自己的,但要放在一行上
    USE 库名;
    DROP FUNCTION IF EXISTS f_test;
    CREATE FUNCTION f_test() RETURNS datetime SQL SECURITY INVOKER BEGIN DECLARE r datetime DEFAULT NOW(); RETURN r; END;
    SELECT f_test();