0

I have the following MySQL trigger query :

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$$

When I run the Query I get the following MySQL error : Error

SQL query:

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

MySQL said: Documentation

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 7

Please can some one help solving the problem?

3
  • Which line in above code is line 7? Commented Nov 25, 2013 at 9:59
  • P5Coder please view the following : SET @changetype = 'DELETE'; Commented Nov 25, 2013 at 10:00
  • @user689017. is deleted a column in your table? conceptually is a little weird to have a changetype of DELETE in a INSERT trigger. :p Commented Nov 25, 2013 at 10:47

3 Answers 3

1

You've missed DELIMITER declaration:

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$$
Sign up to request clarification or add additional context in comments.

2 Comments

After adding the DELIMITER $$ , I get the following error : #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 'ON stockFOR EACH ROW BEGIN IF NEW.deleted THEN SET @changetype = 'DELETE';ELSE' at line 1
In my case, all is working fine (i.e. I'm getting error "Table 'db.stock' doesn't exist" and, thus, syntax is correct). Can you post fiddle?
0

Try this:


  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$$

Comments

0

Try this simplified query -

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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.