2012-06-12 117 views
1

使用MySQL,如何使用phpmyadmin將用戶定義的數據庫函數從一個數據庫服務器導入到另一個數據庫服務器?MySQL,phpmyadmin,如何導入用戶定義的函數

select * from information_schema.routines; 

這裏是我的功能,我想移動到不同的數據庫服務器:

的用戶定義的函數列表可以使用這個SQL是牽強

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 

回答

1

希望這是可能在phpMyAdmin:

  • 執行SHOW CREATE FUNCTION
  • 你會看到CREATE FUNCTION語句,執行它與另一個MySQL服務器
  • 重複此步驟,爲每個功能。

注意,該功能可能有安全選項(定義,SQL SECURITY),瞭解在documentation (CREATE PROCEDURE and CREATE FUNCTION Syntax)這些選項;如果您想使用它們,請檢查指定的用戶是否在另一臺服務器上創建。

SHOW CREATE FUNCTION Syntax

+0

謝謝Devart,我創建了查詢來創建函數,但不知何故它拋出錯誤。我更新了上面的問題,錯誤。任何想法? – Developer

+0

它可以是一箇舊的phpmyadmin版本DE​​LIMITER問題 - http://stackoverflow.com/questions/2846516/how-to-write-a-stored-procedure-using-phpmyadmin-and-how-to-use-it-通過php – Devart

+0

您也可以嘗試使用PHP的MySQL擴展來創建過程。 – Devart

5

嘗試通過添加$,它適用於我。

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$ 
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 ; 
0

爲了導出用戶定義的函數:

在phpMyAdmin打開您的數據庫。之後,點擊「導出」按鈕。在打開的窗口中,應該顯示覆選框(如果未顯示,請選擇「自定義 - 顯示所有可能的選項」): 1)添加DROP TABLE/VIEW/PROCEDURE/FUNCTION/EVENT; 2)添加CREATE PROCEDURE/FUNCTION/EVENT; 檢查並導出它。

只需將其導入到您的數據庫,它工作正常!

P.S.您可以在信息模式數據庫例程表中看到這些函數,但不能直接導入它,因爲即使您是root用戶也沒有權限。

相關問題