简体   繁体   中英

MySQL, phpmyadmin, how to import user defined functions

Using MySQL, How do I import a user defined database function from one db server to another db server using phpmyadmin?

A list of user defined functions can be fetched using this SQL:

select * from information_schema.routines;

Here is My function I want to move to a different database server:

CREATE DEFINER=`XXX`@`%` FUNCTION `BDayDiff`( d1 DATE, d2 DATE ) RETURNS int(11)
   DETERMINISTIC
BEGIN
 DECLARE dow1, dow2, days, wknddays INT;
 SET dow1 = DAYOFWEEK(d1);
 SET dow2 = DAYOFWEEK(d2);
 SET days = DATEDIFF(d2,d1);
 SET wknddays = 2 * FLOOR( days / 7 ) +
                IF( dow1 = 1 AND dow2 > 1, 1,                              
                    IF( dow1 = 7 AND dow2 = 1, 1,              
                        IF( dow1 > 1 AND dow1 > dow2, 2,      
                            IF( dow1 < 7 AND dow2 = 7, 1, 0 )  
                          )
                      )
                  );
 RETURN FLOOR(days - wkndDays);
END

But I get an error:

Error

SQL query:

CREATE DEFINER = `XXX`@`%` FUNCTION `BDayDiff` (
d1 DATE,
d2 DATE
) RETURNS INT( 11 ) DETERMINISTIC BEGIN DECLARE dow1,
dow2,
days,
wknddays INT;

MySQL said: Documentation
#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 '' at line 5 

Try this by adding $, it works for me.

DELIMITER $

CREATE DEFINER=`XXX`@`%` FUNCTION `BDayDiff`( d1 DATE, d2 DATE ) RETURNS int(11)
   DETERMINISTIC
BEGIN
 DECLARE dow1, dow2, days, wknddays INT;
 SET dow1 = DAYOFWEEK(d1);
 SET dow2 = DAYOFWEEK(d2);
 SET days = DATEDIFF(d2,d1);
 SET wknddays = 2 * FLOOR( days / 7 ) +
                IF( dow1 = 1 AND dow2 > 1, 1,                              
                    IF( dow1 = 7 AND dow2 = 1, 1,              
                        IF( dow1 > 1 AND dow1 > dow2, 2,      
                            IF( dow1 < 7 AND dow2 = 7, 1, 0 )  
                          )
                      )
                  );
 RETURN FLOOR(days - wkndDays);
END$

Hope this is possible in phpmyadmin:

  • Execute SHOW CREATE FUNCTION
  • You will see CREATE FUNCTION statement, execute it against another MySQL server
  • Repeat this steps for each function.

Note, that functions may have security options (DEFINER, SQL SECURITY), read about these options in the documentation (CREATE PROCEDURE and CREATE FUNCTION Syntax) ; if you want to use them, check that specified users are created on another server.

SHOW CREATE FUNCTION Syntax .

If you are using phpmyadmin interface to execute the query for function then you should follow the steps as below: 

DELIMITER $$

CREATE FUNCTION `BDayDiff`( d1 DATE, d2 DATE ) RETURNS int(11)
    DETERMINISTIC
BEGIN
 DECLARE dow1, dow2, days, wknddays INT;
 SET dow1 = DAYOFWEEK(d1);
 SET dow2 = DAYOFWEEK(d2);
 SET days = DATEDIFF(d2,d1);
 SET wknddays = 2 * FLOOR( days / 7 ) +
                IF( dow1 = 1 AND dow2 > 1, 1,                              
                    IF( dow1 = 7 AND dow2 = 1, 1,              
                        IF( dow1 > 1 AND dow1 > dow2, 2,      
                            IF( dow1 < 7 AND dow2 = 7, 1, 0 )  
                          )
                      )
                  );
 RETURN FLOOR(days - wkndDays);
END $$

DELIMITER ;

In order to export User-defined functions:

In PhpMyAdmin open your DB. After that click on "Export" button. In opening window it should be folowing checkboxes in (if it's not shown select "Custom - display all possible options"): 1) Add DROP TABLE / VIEW / PROCEDURE / FUNCTION / EVENT; 2) Add CREATE PROCEDURE / FUNCTION / EVENT; Check and export it.

Just import it to your DB and it works fine!

PS You can see those functions in Information Schema DB's Routines table but you can't import it directly because you haven't permission even if you are root user.

If you have exported it from PHPMyAdmin, you need to include at the head of code with

    DELIMITER $
CREATE DEFINER=`root`@`localhost` FUNCTION

DELIMITER ;

this could resolve your issue.

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