将SQL Server存储过程转换为Mysql时出错

时间:2014-04-19 05:00:20

标签: mysql sql sql-server

我正在尝试将SQL Server存储过程转换为Mysql,因为我正在将整个数据库迁移到Mysql

但是我无法转换使用XML交互的一些存储过程。我不是一个Mysql人。有人可以帮帮我吗?

提前致谢。

我在SQL Server中的存储过程如下所示:

ALTER PROCEDURE [dbo].[usp_MemberToDoList_UpdateForMember]
(
    @xml nvarchar(max),
    @login varchar(255)
)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @doc int;
    DECLARE @now datetime = GETUTCDATE();

    EXEC [sp_xml_preparedocument] @doc OUTPUT, @xml;

    UPDATE
        [mtdl]
    SET
        [taskCompleteDate] = CASE WHEN [isCompleted] = CONVERT(bit, 1) THEN @now ELSE NULL END,
        [updatedBy] = @login,
        [dateUpdated] = GETUTCDATE()
    FROM
        [MemberToDoList] [mtdl]
    JOIN
        OPENXML (@doc, '/todos/todo') WITH
    (
        [id] int,
        [isCompleted] bit
    ) [x] ON [x].[id] = [mtdl].[memberToDoListId];

    EXEC [sp_xml_removedocument] @doc;
END

当我转换为Mysql时,它看起来像

CREATE PROCEDURE `conversion`.`usp_MemberToDoList_UpdateForMember` (xml longtext,
    login varchar(255))
BEGIN
    DECLARE v_doc int;
    DECLARE v_now datetime(3);
    set v_now = UTC_TIMESTAMP();

    CALL sp_xml_preparedocument(@doc)
    PREPARE stmt FROM @stmt_str;
     EXECUTE stmt;
     DEALLOCATE PREPARE stmt;
      v_doc OUT, xml;

    UPDATE
        mtdl
    SET
        `taskCompleteDate` = CASE WHEN `isCompleted` = CONVERT(1,UNSIGNED) THEN v_now ELSE NULL END
 ,`updatedBy` = @login,
        `dateUpdated` = UTC_TIMESTAMP()
    FROM
        `MemberToDoList` `mtdl`
    JOIN
        ExtractValue(@doc, '/todos/todo') WITH
    (
        `id` int,
        `isCompleted` bit
    ) `x` ON [x].[id] = `mtdl`.`memberToDoListId`;

    SET @stmt_str =  `sxml_removedocument`;
    PREPARE stmt FROM @stmt_str;
    EXECUTE stmt;`enter code here`
    DEALLOCATE PREPARE stmt; @doc;
END

但让我犯了错误:

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“END”附近使用正确的语法

仅供参考我使用的是Mysql 5.6版

1 个答案:

答案 0 :(得分:0)

试试这个:

你的创建或sql查询,如果你想创建sql过程,你应该在查询的开头和结尾添加delimiter。并;添加类似我的查询END;,因为查询如下:

delimiter //
CREATE PROCEDURE `conversion`.`usp_MemberToDoList_UpdateForMember` (xml longtext,
    login varchar(255))
BEGIN
    DECLARE v_doc int;
    DECLARE v_now datetime(3);
    set v_now = UTC_TIMESTAMP();

    CALL sp_xml_preparedocument(@doc)
    PREPARE stmt FROM @stmt_str;
     EXECUTE stmt;
     DEALLOCATE PREPARE stmt;
      v_doc OUT, xml;

    UPDATE
        mtdl
    SET
        `taskCompleteDate` = CASE WHEN `isCompleted` = CONVERT(1,UNSIGNED) THEN v_now ELSE NULL END
 ,`updatedBy` = @login,
        `dateUpdated` = UTC_TIMESTAMP()
    FROM
        `MemberToDoList` `mtdl`
    JOIN
        ExtractValue(@doc, '/todos/todo') WITH
    (
        `id` int,
        `isCompleted` bit
    ) `x` ON [x].[id] = `mtdl`.`memberToDoListId`;

    SET @stmt_str =  `sxml_removedocument`;
    PREPARE stmt FROM @stmt_str;
    EXECUTE stmt;`enter code here`
    DEALLOCATE PREPARE stmt; @doc;
END; //
delimiter ; 

<强> http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html