这是数据库车辆表的触发器
DROP TRIGGER IF EXISTS InsertVehTrig;
DELIMITER $$
CREATE TRIGGER InsertVehTrig AFTER INSERT
ON Vehicle FOR 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 is null) then
select count(id) into @DevCount from vehicle where (cph=@Cph) or (DevID=@DevID);
-- 条件:当前的车牌号 或 设备ID
end if;
if (DevCount > 1) then -- 如果记录数,超过1,则认为有重复
-- Rollback not supported in trigger
SET @SWV_Null_Var = 0;
Leave SWL_return;
else
if (DevCount = 1) then
select ID INTO @TmpID from Vehicle where (Vehicle.cph = @Cph) or (Vehicle.DevID = @DevID);
if (TmpID != VehID) then -- --如果增加的车牌号码与数据库中的在相同的,则不允许增加
-- Rollback not supported in trigger
Leave SWL_return;
SET @SWV_Null_Var = 0;
end if;
end if;
end if;
update vehicle set cph = @Cph where ID = @VehID;
END;
现在我正试图在车辆表中插入新的数据行,但是这个错误
ERROR 1442: Can't update table 'vehicle' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
SQL Statement:
INSERT INTO `gis_server`.`vehicle` (`TrackerNum`, `cph`, `DevID`, `DevType`) VALUES ('1', 'NR09B00555', 'NR09B00555', '2')
这些数据库是由3方公司设计的,答案 0 :(得分:0)
您可以通过对方法进行两处更改来实现此目的:
BEFORE
触发器而不是AFTER
NEW
表,在NEW
表到达INSERT
所针对的表之前将列值设置为新值。 例如,将UPDATE
行替换为以下内容:
UPDATE NEW SET cph = Cph;
MySQL不允许您编辑在该触发器中创建触发器的表中的数据,但您可以编辑NEW
表以修改进入该表的值。