使用MySQL,如何使用phpmyadmin将用户定义的数据库功能从一个数据库服务器导入另一个数据库服务器?
可以使用此SQL获取用户定义函数的列表:
select * from information_schema.routines;
这是我希望移动到其他数据库服务器的我的函数:
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
但是我收到了一个错误:
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
答案 0 :(得分:6)
通过添加$来试试这个,它对我有用。
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$
答案 1 :(得分:2)
希望在phpmyadmin中可以这样做:
注意,这些函数可能有安全选项(DEFINER,SQL SECURITY),请在documentation (CREATE PROCEDURE and CREATE FUNCTION Syntax)中阅读这些选项;如果要使用它们,请检查是否在另一台服务器上创建了指定的用户。
答案 2 :(得分:0)
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 ;
答案 3 :(得分:0)
为了导出用户定义的函数:
在PhpMyAdmin中打开您的数据库。之后点击&#34;导出&#34;按钮。在打开的窗口中,它应该是下面的复选框(如果它没有显示,请选择&#34;自定义 - 显示所有可能的选项&#34;): 1)添加DROP TABLE / VIEW / PROCEDURE / FUNCTION / EVENT; 2)添加CREATE PROCEDURE / FUNCTION / EVENT; 检查并导出它。
只需将其导入您的数据库即可正常使用!
P.S。您可以在信息架构数据库的例程表中查看这些功能,但即使您是root用户,也无法直接导入它。
答案 4 :(得分:0)
如果您是从PHPMyAdmin导出的,则需要在代码的开头加上
DELIMITER $
CREATE DEFINER=`root`@`localhost` FUNCTION
DELIMITER ;
这可以解决您的问题。