如何在MySQL触发器中使用“WITH”子句?

时间:2017-11-28 19:09:07

标签: mysql sql

我正在尝试编写一个Mysql查询。我在select函数的WITH子句上遇到一些错误。

这是示例查询:

CREATE TRIGGER  SAMPLE BEFORE INSERT ON SERVER
  FOR EACH ROW
  BEGIN
    IF ((SELECT COUNT(S.ID) FROM SERVER S WITH UR) >= (SELECT L.SERVERS FROM SAMPLE L WHERE L.ID = 1 LIMIT 1)) THEN
        SIGNAL SQLSTATE '73550' SET MESSAGE_TEXT='+ID';
    END IF;
  END;

我收到了错误:

right syntax to use near 'UR) >= (SELECT L.SERVERS FROM SAMPLE L WHERE L.ID = 1 LIMIT 1))

在Mysql中不支持WITH子句吗?或者我应该使用任何其他语法?任何建议都会有所帮助。

更新:

我也在使用其他查询:

CREATE TRIGGER SAMPLE_TRIGGER NO CASCADE BEFORE INSERT ON TABLE_1
  FOR EACH ROW
  BEGIN
      SET NEW.RID = (SELECT ID FROM TABLE_2 WHERE ACTIVE=0 FETCH FIRST 1 ROWS ONLY WITH RS USE AND KEEP EXCLUSIVE LOCKS);
      IF (NEW.RID IS NULL) THEN
          SIGNAL SQLSTATE '73550' SET MESSAGE_TEXT='+ID';
      END IF;
  END;

得到另一个错误

right syntax to use near 'LIMIT 1 WITH RS USE AND KEEP EXCLUSIVE LOCKS);
      IF (NEW.RID IS NULL) THEN'

1 个答案:

答案 0 :(得分:1)

在DB2中,with ur表示“使用未提交的读取”。这是数据库中的锁定机制,如here所述。

如果要将代码移植到MySQL,我不担心这个。所以,只需删除它:

CREATE TRIGGER  SAMPLE BEFORE INSERT ON SERVER
  FOR EACH ROW
  BEGIN
    IF ((SELECT COUNT(S.ID) FROM SERVER S) >= (SELECT L.SERVERS FROM SAMPLE L WHERE L.ID = 1 LIMIT 1)) THEN
        SIGNAL SQLSTATE '73550' SET MESSAGE_TEXT='+ID';
    END IF;
  END;