CREATE FUNCTION error “This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA”

前端 未结 7 823
走了就别回头了
走了就别回头了 2021-01-01 17:17

Our database has a function to generate an order number. It reads a value from a Settings table, increments it, then returns the new value. For example:

CREA         


        
相关标签:
7条回答
  • 2021-01-01 17:24

    add READS SQL DATA which declare that is a read only function :

    CREATE FUNCTION NextOrderNumber() RETURNS INTEGER UNSIGNED NOT DETERMINISTIC
    READS SQL DATA
    BEGIN
      DECLARE number INTEGER UNSIGNED;
      UPDATE Settings SET IntegerValue=LAST_INSERT_ID(IntegerValue+1) WHERE KeyName='NextOrderNumber';
      SET number=LAST_INSERT_ID();
      return number;
    END
    
    0 讨论(0)
  • 2021-01-01 17:25

    Execute this just before creating the function:

    SET @@global.log_bin_trust_function_creators = 1;
    

    And add MODIFIES SQL DATA to the declaration.

    Also... well, you asked not to comment the function itself, but I suggest that you drop the number variable and simply do RETURN LAST_INSERT_ID().

    0 讨论(0)
  • 2021-01-01 17:28

    Could I instead just flag the function as NO SQL to suppress the warning? I tried it and it worked. Will this cause any problem?

    According to this Mysql doc:

    Assessment of the nature of a function is based on the “honesty” of the creator: MySQL does not check that a function declared DETERMINISTIC is free of statements that produce nondeterministic results.

    So it's up to you. If you are sure the method won't cause any problem...

    0 讨论(0)
  • 2021-01-01 17:31

    I've googled and here I am. I've found a way :

    SET GLOBAL log_bin_trust_function_creators = 1;
    

    But be careful, it may be unsafe for data recovery or replication...

    0 讨论(0)
  • 2021-01-01 17:34

    There are two ways to fix this:

    Execute the following in the MySQL console:

    SET GLOBAL log_bin_trust_function_creators = 1;
    

    Add the following to the mysql.ini configuration file:

    log_bin_trust_function_creators = 1
    

    The setting relaxes the checking for non-deterministic functions. Non-deterministic functions are functions that modify data (i.e. have update, insert or delete statement(s)). For more info, see here.

    Please note, if binary logging is NOT enabled, this setting does not apply.

    0 讨论(0)
  • 2021-01-01 17:41

    As per my understating it cause problem when data recovery or replication

    Ref: http://dev.mysql.com/doc/refman/5.0/en/stored-programs-logging.html

    MySQL 5.0.6: Statements that create stored routines and CALL statements are logged. Stored function invocations are logged when they occur in statements that update data (because those statements are logged).

    However, function invocations are not logged when they occur in statements such as SELECT that do not change data, even if a data change occurs within a function itself; this can cause problems.

    Under some circumstances, functions and procedures can have different effects if executed at different times or on different (master and slave) machines, and thus can be unsafe for data recovery or replication.

    E.g.

    CREATE FUNCTION myfunc () RETURNS INT DETERMINISTIC
    BEGIN
      INSERT INTO t (i) VALUES(1);
      RETURN 0;
    END;
    
    SELECT myfunc();
    

    If a stored function is invoked within a statement such as SELECT that does not modify data, execution of the function is not written to the binary log, even if the function itself modifies data. This logging behavior has the potential to cause problems. Suppose that a function myfunc() is defined as above.

    0 讨论(0)
提交回复
热议问题