[Err] 1442 - Can't update table 'test_trigger' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

DROP TABLE IF EXISTS `test_trigger`;
CREATE TABLE `test_trigger` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`jb` double DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`dtype` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
DROP TRIGGER IF EXISTS `t_trigger`;
DELIMITER ;;
CREATE TRIGGER `t_trigger` BEFORE INSERT ON `test_trigger` FOR EACH ROW BEGIN
-- sum jb.. IF new.dtype = 'touch' THEN
-- get sum row id
SET @tmp_id = NULL ; SELECT
MAX(id) INTO @tmp_id
FROM
test_trigger
WHERE
`name` = NEW.`name`
AND dtype = 'sum' ; -- insert or update row IF @tmp_id IS NULL THEN
insert into test_trigger (jb,dtype) values( new.jb ,'sum') ;
ELSE
UPDATE test_trigger SET jb = jb +new.jb WHERE id = @tmp_id;
END IF; END
IF ; END
;;
DELIMITER ;
[Err] 1442 - Can't update table 'test_trigger' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

导致原因是

  在一张表中的触发器,不能直接对同一张表执行 增加,删除,修改操作,防止造成死循环(个人理解)。
借助连接查询语句试试看,实现思路,先将要符合删除条件的记录查询出来,作为一个新的集合(相当于一张中间表),在通过集合与当前表的 inner join  语句 进行删除 (记得之前用过,好像可以,记得不是很清楚了)

Can't update table 'test_trigger' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.的更多相关文章

  1. Can’t update table ‘xxx’ in stored function/trigger because it is already used by statement which invoked this stored function/trigger

    MySQL: Solution for ERROR 1442 (HY000): Can't update table 'xxx' in stored function/trigger because ...

  2. ERROR 1442 (HY000):because it is already used by statement which invoked this stored function/tr

    看到mysql的触发器,随手写了一个: mysql> create trigger t_ai_test -> after insert on test -> for each row ...

  3. MySQL - 问题集 - 触发器更新本表数据异常"Can’t update table ‘tbl’ in stored function/trigger because it is already used by statement which invoked this"

    如果你在触发器里面对刚刚插入的数据进行了 insert/update, 则出现这个问题.因为会造成循环的调用. create trigger test before update on test fo ...

  4. MySql Error: Can't update table in stored function/trigger

    MySql Error: Can't update table in stored function/trigger because it is already used by statement w ...

  5. MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it

    MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it 博客分类: 数据库 MySQLJava ...

  6. LLBLGen update table with join

    Table1 id Name 1 xxx 2 ooo Table2 Table1Id Table1Name Column1 Column2 Column3 1 sss xxxx xxxx xxxx 2 ...

  7. 【sql技巧】mysql修改时,动态指定要修改的字段 update `table` set (case when ....) = 1 where id = xx

    如果你点进了这篇帖子,那么你一定遇到了跟我一样的问题.别看题目的set case when...,我一开始也是第一反应是用case when但是发现并不好使. 问题呢,说得高大上一点:动态指定要修改的 ...

  8. 关于Mysql 触发器

    首先,测试版本 Mysql 5.6. 然后再看触发器的语法 CREATE [DEFINER = { user | CURRENT_USER }] TRIGGER trigger_name trigge ...

  9. 对数据库触发器new和old的理解

    在数据库的触发器中经常会用到更新前的值和更新后的值,所有要理解new和old的作用很重要.当时我有个情况是这样的:我要插入一行数据,在行要去其他表中获得一个单价,然后和这行的数据进行相乘的到总金额,将 ...

随机推荐

  1. RC4 加密算法asp版

    Function RC4(data, Key) ), k(), outstr, Acii, j, tst, xre1, xre2, temp, x, t, y, qwe, zxc s(i) = i - ...

  2. Part 7Handling events in AngularJS

    Let us understand with an example. Here is what we want to do. 1. Display the list of technologies i ...

  3. 怎么手写Ajax实现异步刷新

    所谓的异步刷新,就是不刷新整个网页进行更新数据. 只有通过js才能实现Ajax,进而实行异步刷新 表单提交数据和Ajax提交数据的区别:表单提交是提交的整个页面中的数据,提交数据之后会抛弃之前的页面( ...

  4. 谷歌浏览器Chrome不再支持showModalDialog的解决办法

    问题重现 弹出窗口编码: JavaScript 0 1 2 3 4 5 6 7   var obj = new Object();   var retval = window.showModalDia ...

  5. 获取字符串对应的MD5值 (AL16UTF16LE)

    CREATE OR REPLACE FUNCTION fn_md5_utf16le (InputString IN VARCHAR2) RETURN VARCHAR2 IS retval ); /** ...

  6. IOS开发: 为UIImageView添加点击事件

    转载于:http://www.pocketdigi.com/20140218/1276.html UIImageView并不像UIButton一样,点点鼠标就可以关联点击事件,也不像Android里有 ...

  7. 常见web服务器错误

    参考地址:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5     10 Status Code Definitions ...

  8. OpenJudge 666:放苹果

    总时间限制: 1000ms 内存限制: 65536kB 描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. 输 ...

  9. 素数个数统计——Eratosthenes筛法 [LeetCode 204]

    1- 问题描述 Count the number of prime numbers less than a non-negative number, n 2- 算法思想 给出要筛数值的范围 $n$,找 ...

  10. 《gpg文件加密的使用》RHEL6

    甲端: 首先是要生成一对密钥: 提示是否要生成2048个字节的密钥对:   下面都是生成密钥对时的步骤: 按“o”键开始生成密钥对: 提示要我给密钥对加个密码: 输入2次 之后密钥对的字符需要我按键盘 ...