How to Insert Data with Mysql Trigger After Insert? -
this database vehicle table's trigger
drop trigger if exists insertvehtrig; delimiter $$ create trigger insertvehtrig after insert on vehicle each row swl_return: begin declare cph char(50); declare devid char(12); declare vehid bigint; declare tmpid bigint; declare devcount int; set cph = rtrim(ltrim(new.cph)); set vehid = new.id; set devid = new.devid; if(vehid null) select count(id) @devcount vehicle (cph=@cph) or (devid=@devid); -- 条件:当前的车牌号 或 设备id end if; if (devcount > 1) -- 如果记录数,超过1,则认为有重复 -- rollback not supported in trigger set @swv_null_var = 0; leave swl_return; else if (devcount = 1) select id @tmpid vehicle (vehicle.cph = @cph) or (vehicle.devid = @devid); if (tmpid != vehid) -- --如果增加的车牌号码与数据库中的在相同的,则不允许增加 -- rollback not supported in trigger leave swl_return; set @swv_null_var = 0; end if; end if; end if; update vehicle set cph = @cph id = @vehid; end;
right m trying insert new data row in vehicle table, error this
error 1442: can't update table 'vehicle' in stored function/trigger because used statement invoked stored function/trigger. sql statement: insert `gis_server`.`vehicle` (`trackernum`, `cph`, `devid`, `devtype`) values ('1', 'nr09b00555', 'nr09b00555', '2')
those database designed 3 party company,
how insert data vehicle table?
you can achieve making 2 changes approach:
- firstly, use
before
trigger ratherafter
- secondly, rather updating same table, update
new
table, setting column value new value beforenew
table hits table targetedinsert
.
for example, replace update
line following:
update new set cph = cph;
mysql doesn't allow edit data in table on trigger created in trigger, can edit new
table modify values going table.
Comments
Post a Comment