创建触发器时MYSQL错误

时间:2013-11-25 09:56:29

标签: mysql sql triggers

我有以下MySQL触发器查询:

CREATE
TRIGGER `after_insert_stock` AFTER INSERT 
ON `stock` 
FOR EACH ROW BEGIN

    IF NEW.deleted THEN
        SET @changetype = 'DELETE';
    ELSE
        SET @changetype = 'NEW';
    END IF;
            INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, @changetype);

END$$

当我运行查询时,我收到以下MySQL错误:错误

SQL查询:

CREATE TRIGGER `after_insert_stock` AFTER INSERT ON `stock_audit`
FOR EACH
ROW BEGIN
IF NEW.deleted
THEN
SET @changetype = 'DELETE';

MySQL说:文档

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第7行的''附近使用正确的语法

有人可以帮助解决问题吗?

3 个答案:

答案 0 :(得分:1)

你错过了DELIMITER声明:

DELIMITER $$
CREATE
TRIGGER `after_insert_stock` AFTER INSERT 
ON `stock` 
FOR EACH ROW BEGIN

    IF NEW.deleted THEN
        SET @changetype = 'DELETE';
    ELSE
        SET @changetype = 'NEW';
    END IF;
            INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, @changetype);

END$$

答案 1 :(得分:0)

试试这个:


  DELIMITER $$
    CREATE TRIGGER after_insert_stock AFTER INSERT ON stock FOR EACH ROW BEGIN 
    DECLARE changetype varchar;
    IF NEW.deleted THEN
        SET changetype := 'DELETE';
    ELSE
        SET changetype := 'NEW';
    END IF;
    INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, :changetype);
  END$$

答案 2 :(得分:0)

试试这个简化的查询 -

CREATE TRIGGER `after_insert_stock`
AFTER INSERT 
ON `stock` 
FOR EACH ROW
INSERT INTO
  stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype)
VALUES
  (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, IF(NEW.deleted, 'DELETE', 'NEW'));