简体   繁体   English

使用存储过程的IF语句的语法错误已从MS-SQL转换为MySQL

[英]Syntax error on IF statement using stored procedure converted from MS-SQL to MySQL

I am converting a stored procedure from MS-SQL to MySQL. 我正在将存储过程从MS-SQL转换为MySQL。 It is based around Directed Acyclic Graphs but I am getting a syntax error. 它基于有向无环图,但出现语法错误。

The original MS-SQL script is in Listing 2 on the following page: http://www.codeproject.com/Articles/22824/A-Model-to-Represent-Directed-Acyclic-Graphs-DAG-o 原始MS-SQL脚本在下一页的清单2中: http : //www.codeproject.com/Articles/22824/A-Model-to-Represent-Directed-Acyclic-Graphs-DAG-o

The error I get is: #1064 - You have an error in your SQL syntax; 我得到的错误是:#1064-您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE varId int; 查看与您的MySQL服务器版本相对应的手册,以在'DECLARE varId int附近使用正确的语法; INSERT INTO edges ( startVertex, en' at line 36 插入边(startVertex,en'在第36行

The MySQL code: 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 ;

This works fine without the IF statements so I think my syntax is a bit wrong. 在没有IF语句的情况下,此方法可以正常工作,因此我认为我的语法有点错误。 Any ideas? 有任何想法吗?

As stated under DECLARE Syntax : DECLARE语法中所述

DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements. 仅在BEGIN ... END复合语句中允许使用DECLARE并且必须在其开始处以及其他任何语句之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM