2
我有一個用於排序類別的「訂單」列的類別表。 我想在觸發之前插入遞增設置順序:如果在觸發器中爲空,則設置變量
CREATE TRIGGER `categories_set_order` BEFORE INSERT ON `categories` FOR EACH ROW BEGIN
DECLARE max_order INT DEFAULT 0;
SELECT MAX(categories.order) INTO max_order FROM categories;
SET NEW.order = max_order + 1;
END;
但是如果在數據庫中沒有記錄,訂單列被設置爲NULL。 我已經修改了觸發代碼:
CREATE TRIGGER `categories_set_order` BEFORE INSERT ON `categories` FOR EACH ROW BEGIN
DECLARE max_order INT DEFAULT 0;
SELECT MAX(categories.order) INTO max_order FROM categories;
IF (ISNULL(@max_order)) THEN BEGIN
SET max_order = 0;
END;
SET NEW.order = max_order + 1;
END;
And I'm getting the following error:
Error Code: 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 9
線9:
IF(ISNULL(@max_order))然後開始
我試着刪除 「@」,但仍然得到相同的錯誤。如何解決它?