0

ストアドプロシージャをMS-SQLからMySQLに変換しています。有向非巡回グラフに基づいていますが、構文エラーが発生します。

元のMS-SQLスクリプトは、次のページのリスト2にあります。http: //www.codeproject.com/Articles/22824/A-Model-to-Represent-Directed-Acyclic-Graphs-DAG-o

私が得るエラーは次のとおりです。#1064-SQL構文にエラーがあります。'DECLARE varId intの近くで使用する正しい構文については、MySQLサーバーのバージョンに対応するマニュアルを確認してください。INSERT INTO edge(startVertex、en'at line 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ステートメントがなくても正常に機能するため、構文が少し間違っていると思います。何か案は?

4

1 に答える 1

1

DECLARE構文で述べたように:

DECLARE複合ステートメント内でのみ許可されBEGIN ... END、他のステートメントの前に開始する必要があります。

于 2012-08-30T14:16:26.017 に答える