2012-08-30 50 views
0

我正在將存儲過程從MS-SQL轉換爲MySQL。它基於Directed Acyclic Graphs,但是我得到一個語法錯誤。使用從MS-SQL轉換到MySQL的存儲過程的IF語句的語法錯誤

原始MS-SQL腳本清單2在下頁中: http://www.codeproject.com/Articles/22824/A-Model-to-Represent-Directed-Acyclic-Graphs-DAG-o

我得到的錯誤是: #1064 - 你在你的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以在'DECLARE varId int; INSERT INTO邊緣(startVertex,連接」在第36行

的MySQL代碼:

DELIMITER // 
CREATE PROCEDURE AddEdge(
IN iStartVertexId varchar(36), 
IN iEndVertexId varchar(36), 
IN iSource varchar(150) 
) 

MAIN_BLOCK: BEGIN 

DECLARE counter int default 0; 
SET counter = (SELECT id 
    FROM edges 
    WHERE startVertex = iStartVertexId 
    AND endVertex = iEndVertexId 
    AND hops = 0); 
IF counter > 0 THEN 
    BEGIN 
     LEAVE MAIN_BLOCK; 
    END; 
END IF; 

SET counter = 0; 
SET counter = (SELECT Id 
        FROM edges 
        WHERE StartVertex = @EndVertexId 
         AND EndVertex = @StartVertexId); 

IF iStartVertexId = iEndVertexId 
     OR counter > 0 
THEN 
BEGIN 

    LEAVE MAIN_BLOCK; 
END; 
END IF; 


DECLARE varId int; 

INSERT INTO edges (
    startVertex, 
    endVertex, 
    hops, 
    source) 
    VALUES (
    iStartVertexId, 
    iEndVertexId, 
    0, 
    iSource); 

SELECT varId = LAST_INSERT_ID(); 
UPDATE edges 
    SET entryEdgeId = varId 
    , exitEdgeId = varId 
    , directEdgeId = varId 
    WHERE id = varId; 

-- step 1: A's incoming edges to B 
INSERT INTO edges (
    entryEdgeId, 
    directEdgeId, 
    exitEdgeId, 
    startVertex, 
    endVertex, 
    hops, 
    source) 
    SELECT id 
    , varId 
    , varId 
    , startVertex 
    , iEndVertexId 
    , hops + 1 
    , iSource 
    FROM edges 
    WHERE endVertex = iStartVertexId; 

-- step 2: A to B's outgoing edges 
INSERT INTO edges (
    entryEdgeId, 
    directEdgeId, 
    exitEdgeId, 
    startVertex, 
    endVertex, 
    hops, 
    source) 
    SELECT varId 
    , varId 
    , id 
    , iStartVertexId 
    , endVertex 
    , hops + 1 
    , iSource 
    FROM edges 
    WHERE startVertex = iEndVertexId; 

-- step 3: A’s incoming edges to end vertex of B's outgoing edges 
INSERT INTO edges (
    entryEdgeId, 
    directEdgeId, 
    exitEdgeId, 
    startVertex, 
    endVertex, 
    hops, 
    source) 
    SELECT A.id 
    , varId 
    , B.id 
    , A.startVertex 
    , B.endVertex 
    , A.hops + B.hops + 1 
    , iSource 
    FROM edges A 
    CROSS JOIN edges B 
    WHERE A.endVertex = iStartVertexId 
    AND B.startVertex = iEndVertexId; 

END // 
DELIMITER ; 

這正常不IF語句,所以我想我的語法是有點不對任何想法

+0

您可能想要考慮使用觸發器,而不是SP:如果定義正確,您將能夠直接對底層表進行更改並讓MySQL自動維護DAG。 – eggyal

回答

1

?作爲DECLARE Syntax下稱:

DECLARE只允許內部BEGIN ... END複合語句,並且必須是在它的開始,任何其他語句之前

+0

再次感謝Eggyal :) 更驚訝的是,在我的代碼的唯一錯誤,儘管我認爲語法可能是正確的,但並不意味着它會工作;) –

+1

@JamesPitt:說實話,我沒有看過你的代碼 - 只是指出了導致您提到的語法錯誤的原因:)要更全面地查看代碼,您應該嘗試[codereview.se]。 – eggyal

+0

很酷。我以前沒有看過那個網站。好一個! –