Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL after insert trigger get auto incremed value, update field value after insert gives "Unknown column" error

Tags:

mysql

triggers

I am trying to figure out make a trigger to assign the value of the auto incremented 'ID' primary key field that is auto generated upon insert to another field 'Sort_Placement' so they are the same after insert.

If you are wondering why I am doing this, 'Sort_Placement' is used as a sort value in a table that can be changed but by default the record is added to the bottom of the table

Table Data

`ID` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`Account_Num` mediumint(8) unsigned NOT NULL,
`Product_Num` mediumint(8) unsigned NOT NULL,
`Sort_Placement` mediumint(8) unsigned DEFAULT NULL,
`Order_Qty_C` smallint(6) NOT NULL DEFAULT '0',
`Order_Qty_B` smallint(6) NOT NULL DEFAULT '0',
`Discount` decimal(6,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`ID`),
UNIQUE KEY `ID_UNIQUE` (`ID`)

After Insert Trigger

CREATE 
TRIGGER `order_guide_insert_trigger`
AFTER INSERT ON `order_guide`
FOR EACH ROW
    BEGIN
    IF Sort_Placement IS NULL THEN
        SET Sort_Placement = NEW.ID;
    END IF;
END;

I have tried a bunch of combinations of using the "NEW" prefix with no luck. For example putting the NEW prefix before each field name.

Trying it out

INSERT INTO `order_guide` (`Account_Num`, `Product_Num`) VALUES ('5966', '3');

Insert Error

ERROR 1054: Unknown column 'Sort_Placement' in 'field list'
like image 783
LukeS Avatar asked May 16 '13 21:05

LukeS


People also ask

How to create aftermysql after insert trigger?

MySQL AFTER INSERT triggers are automatically invoked after an insert event occurs on the table. The following shows the basic syntax of creating a MySQL AFTER INSERT trigger: First, specify the name of the trigger that you want to create after the CREATE TRIGGER keywords. Second, use AFTER INSERT clause to specify the time to invoke the trigger.

How to set a value to a column after insert?

The way you are trying to set value to a column is an update. Because you are doing it after insert operation is completed. You actually need a before trigger. And to assign the same new auto incremented value of primary key column of same table, you better get it from information_schema.tables.

How to get the value of column that uses Auto_increment after insert?

Obtaining the value of column that uses AUTO_INCREMENT after an INSERT statement can be achieved in a number of different ways. To obtain the value immediately after an INSERT , use a SELECT query with the LAST_INSERT_ID () function.

Can we change the new values in SQL after insert trigger?

Code language:SQL (Structured Query Language)(sql) In an AFTER INSERTtrigger, you can access the NEWvalues but you cannot change them. Also, you cannot access the OLDvalues because there is no OLDon INSERTtriggers.


1 Answers

This seems like a bit of a hack job but I was able to get it working using the LAST_INSERT_ID() function built into MySQL.

CREATE TRIGGER `order_guide_insert_trigger`
BEFORE INSERT ON `order_guide`
FOR EACH ROW 
BEGIN
    IF NEW.Sort_Placement IS NULL THEN
        SET NEW.Sort_Placement = LAST_INSERT_ID() + 1;
    END IF;
END;

This also works and seems to work

CREATE TRIGGER `order_guide_insert_trigger`
BEFORE INSERT ON `order_guide`
FOR EACH ROW 
BEGIN
    IF NEW.Sort_Placement IS NULL THEN
        SET NEW.Sort_Placement = (SELECT ID FROM order_Guide ORDER BY id DESC LIMIT 1) + 1;
    END IF;
END;
like image 129
LukeS Avatar answered Sep 18 '22 00:09

LukeS