创建触发器:ERROR 1064(42000):SQL语法中有错误;

时间:2016-09-09 12:32:01

标签: mysql stored-procedures triggers

在我看来,语法一切正常,但我得到了错误。

请注意:我正在通过在mysql控制台中粘贴此代码来创建触发器。不确定这是否重要。

DELIMITER $$
CREATE TRIGGER after_nse_fo_insert AFTER INSERT
ON NSE_FO
FOR EACH ROW
BEGIN
DECLARE settle_orders_data VARCHAR(1000) DEFAULT '';
DECLARE delimiter_pos INT DEFAULT 0;
DECLARE start_pos INT DEFAULT 0;
DECLARE fill_id VARCHAR(50) DEFAULT '';
DECLARE if_settled VARCHAR(50) DEFAULT '';
DECLARE units_settled INT DEFAULT 0;
DECLARE temp_diff INT DEFAULT 0;

CALL settle_trade_orders(NEW.FillId, NEW.SymbolName, NEW.TransactionType, NEW.AccountName, NEW.TradeDateTime, CAST(NEW.FillSize AS UNSIGNED), NEW.FillPrice, settle_orders_data);

settling_loop: LOOP

SET start_pos = delimiter_pos + 1;
SET delimiter_pos = LOCATE(',', settle_orders_data, start_pos);

IF delimiter_pos = 0 THEN
    LEAVE settling_loop;
END IF;

SET temp_diff = delimiter_pos - start_pos;
SET fill_id = SUBSTRING(settle_orders_data, start_pos, temp_diff);

SET start_pos = delimiter_pos + 1;
SET delimiter_pos = LOCATE(',', settle_orders_data, start_pos);
SET temp_diff = delimiter_pos - start_pos;
SET if_settled = SUBSTRING(settle_orders_data, start_pos, temp_diff);

SET start_pos = delimiter_pos + 1;
SET delimiter_pos = LOCATE(',', settle_orders_data, start_pos);
SET temp_diff = delimiter_pos - start_pos;
SET settled_count = SUBSTRING(settle_orders_data, start_pos, temp_diff);

-- settle in NSE_FO table
-- UPDATE NSE_FO
-- SET Settled=if_settled, settled_units=settled_count
-- WHERE FillId=fill_id;
END LOOP settling_loop;

END $$
DELIMITER ;

以下是我看到的错误

  

错误1064(42000):您的SQL语法有错误;检查   手册,对应右边的MySQL服务器版本   在'IF;

附近使用的语法      

SET temp_diff = delimiter_pos - start_pos; SET fill_id =   SUBSTRING(第19行的sett_o'

我不确定这些行的语法问题是什么?

更新:完全删除了IF声明。另一个错误抛出。

  

错误1064(42000):您的SQL语法有错误;检查   手册,对应右边的MySQL服务器版本   在'(settle_orders_data,start_pos,temp_diff)附近使用的语法;组   start_pos = delimiter_pos + 1; '在第17行。完全删除了IF。

1 个答案:

答案 0 :(得分:1)

最后一个答案是完全错误地吠叫,因此我已将其删除了。

此代码运行时没有错误,请注意评论语句,因为我不希望您DROP NSE_FO意外。

编辑:这似乎是文本编辑器和控制台之间的复制/粘贴错误。不知道这是怎么发生的,但是我发现Notepad++(在Windows上)过去非常稳定。

此致

詹姆斯

USE test;

/*
DROP TABLE IF EXISTS NSE_FO;
CREATE TABLE NSE_FO (FillId BIT, SymbolName BIT, TransactionType BIT, AccountName BIT, TradeDateTime BIT, FillSize BIT, FillPrice BIT);
*/

DROP TRIGGER IF EXISTS after_nse_fo_insert;

DELIMITER $$
CREATE TRIGGER after_nse_fo_insert AFTER INSERT
ON NSE_FO
FOR EACH ROW
BEGIN
DECLARE settle_orders_data VARCHAR(1000) DEFAULT '';
DECLARE settled_count VARCHAR(1000) DEFAULT '';
DECLARE delimiter_pos INT DEFAULT 0;
DECLARE start_pos INT DEFAULT 0;
DECLARE fill_id VARCHAR(50) DEFAULT '';
DECLARE if_settled VARCHAR(50) DEFAULT '';
DECLARE units_settled INT DEFAULT 0;
DECLARE temp_diff INT DEFAULT 0;

CALL settle_trade_orders(NEW.FillId, NEW.SymbolName, NEW.TransactionType, NEW.AccountName, NEW.TradeDateTime, CAST(NEW.FillSize AS UNSIGNED), NEW.FillPrice, settle_orders_data);

settling_loop: LOOP

SET start_pos = delimiter_pos + 1;
SET delimiter_pos = LOCATE(',', settle_orders_data, start_pos);

IF delimiter_pos = 0 THEN
    LEAVE settling_loop;
END IF;

SET temp_diff = delimiter_pos - start_pos;
SET fill_id = SUBSTRING(settle_orders_data, start_pos, temp_diff);

SET start_pos = delimiter_pos + 1;
SET delimiter_pos = LOCATE(',', settle_orders_data, start_pos);
SET temp_diff = delimiter_pos - start_pos;
SET if_settled = SUBSTRING(settle_orders_data, start_pos, temp_diff);

SET start_pos = delimiter_pos + 1;
SET delimiter_pos = LOCATE(',', settle_orders_data, start_pos);
SET temp_diff = delimiter_pos - start_pos;
SET settled_count = SUBSTRING(settle_orders_data, start_pos, temp_diff);

-- settle in NSE_FO table
-- UPDATE NSE_FO
-- SET Settled=if_settled, settled_units=settled_count
-- WHERE FillId=fill_id;
END LOOP settling_loop;

END $$
DELIMITER ;