Developer
Developer

Reputation: 26173

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 

Upvotes: 3

Views: 8224

Answers (5)

Pranav Bhatt
Pranav Bhatt

Reputation: 745

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.

Upvotes: 1

Bahriddin Abdiev
Bahriddin Abdiev

Reputation: 338

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!

P.S. 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.

Upvotes: 0

bikashfullstack
bikashfullstack

Reputation: 165

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 ;

Upvotes: 0

Developer
Developer

Reputation: 26173

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$

Upvotes: 6

Devart
Devart

Reputation: 121902

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.

Upvotes: 2

Related Questions