简体   繁体   English

将 MS-SQL 存储过程转换为 MYSQL,但现在由于语法错误而无法创建

[英]Converted MS-SQL stored proc to MYSQL but now it wont create due to syntax errors

I have a strong MS-SQL background but have never seen a line of mysql until yesterday.我有很强的 MS-SQL 背景,但直到昨天才看到一行 mysql。 I need to convert my ms-sql stored proc to mysql and I thought I did it correctly BUT when I try to execute the sql to create the proc I just get incorrect syntax errors like this " #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 10".我需要将我的 ms-sql 存储过程转换为 mysql,我认为我做对了,但是当我尝试执行 sql 来创建过程时,我只是收到不正确的语法错误,例如“#1064 - 你的 SQL 有错误语法;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 10 行“//”附近使用的正确语法。 The stored proc is super simple so I feel dumb that I havent been able to figure this out.存储过程非常简单,所以我无法弄清楚这一点,我感到很愚蠢。 BTW, the recipe table has an identity on it called ID which is why I am using scope_identity.顺便说一句,配方表上有一个名为 ID 的标识,这就是我使用 scope_identity 的原因。

MS-SQL微软SQL

CREATE PROCEDURE ins_recipe
  @Name varchar(8000),
  @Directions varchar(8000);
AS
  IF NOT EXISTS(SELECT * FROM recipe WHERE Name = @Name)
    BEGIN
      INSERT INTO recipe ('name', 'directions')
      SELECT @Name, @Directions;

      SELECT SCOPE_IDENTITY;
    END
  ELSE
    BEGIN
      UPDATE recipe
        SET Directions = @Directions
      WHERE Name = @Name;

      SELECT ID FROM recipe WHERE Name = @Name;
    END
GO

I dont know how to do it all in mysql so this is just my starting attempt我不知道如何在 mysql 中完成这一切,所以这只是我的开始尝试

DELIMITER //

CREATE PROCEDURE ins_recipe (IN
  p_Name varchar(8000),
  p_Directions varchar(8000))
BEGIN
  INSERT INTO `recipe` (Name, Directions) 
  SELECT * FROM (SELECT p_Name, p_Directions) AS tmp
  WHERE NOT EXISTS(SELECT Name FROM `recipe` WHERE Name = p_Name) LIMIT 1;

  SELECT LAST_INSERT_ID();
END //

Any help would be mucho appreciated.任何帮助将不胜感激。 The error I get is that there is incorrect syntax near // and to refer to my correct mysql documentation.我得到的错误是 // 附近有不正确的语法,并参考我正确的 mysql 文档。 If anyone feels like helping me convert that whole procedure that would also be much appreciated but getting past the syntax errors at this point is my priority.如果有人想帮助我转换整个过程,那也将不胜感激,但此时克服语法错误是我的首要任务。

For MySQL, I would suggest the following.对于 MySQL,我建议如下。 First, create a unique index/constraint, to prevent duplicate names:首先,创建一个唯一的索引/约束,以防止重名:

create unique index unq_recipe_name on recipe(name);

Then, write the function using on duplicate key insert :然后,使用on duplicate key insert编写函数:

DELIMITER //

CREATE PROCEDURE ins_recipe (
    IN p_Name varchar(8000),
    IN p_Directions varchar(8000)
   )
BEGIN
    INSERT INTO `recipe` (Name, Directions) 
        VALUES (p_Name, p_Directions)
        ON DUPLICATE KEY UPDATE name = p_name;  -- this is a no op
    SELECT LAST_INSERT_ID();
END; //

The advantage to this approach is that the database maintains the data integrity.这种方法的优点是数据库保持了数据的完整性。 Without the index, two separate processes could insert two recipes with the same name into the database.如果没有索引,两个单独的进程可以将两个具有相同名称的配方插入到数据库中。

How does this work?这是如何运作的? If a duplicate is found, then the ON DUPLICATE KEY clause is invoked.如果找到重复项,则调用ON DUPLICATE KEY子句。 The name = p_name is a no-op, because name already has that value. name = p_name是空操作,因为name已经具有该值。

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

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